1.文件常见方法
boolean flag=f.exists(); //文件是否存在
flag=f.isFile(); //是否是文件
flag=f.isDirectory(); //是否是目录
str=f.getPath(); //获得文件的相对路径
str=f.getAbsolutePath(); //获得文件的绝对路径
str=f.getName(); //获得文件名
flag=f.delete(); //删除文件
flag=f.createNewFile(); //创建文件
long=f.length(); //返回文件长度
注意:File不能操作文件内容
| |
|
|
public static void main(String[] args) {
//File f=new File("文件路径") //"\\"表示转义\ //"/"表示/ File f=new File("e:/a.txt"); System.out.println(f); //文件是否存在 boolean flag=f.exists(); System.out.println(flag); //是否是文件 flag=f.isFile(); System.out.println(flag); //是否是目录 flag=f.isDirectory(); System.out.println(flag);
//获取文件相对路径 String path=f.getPath(); System.out.println(path); //获取文件的绝对路径 path=f.getAbsolutePath(); System.out.println(path);
//获取名字 String name=f.getName(); System.out.println(name); //删除文件或者目录 flag=f.delete(); System.out.println(flag);
//创建(检查异常) try { flag=f.createNewFile(); System.out.println(flag); } catch (IOException e) { e.printStackTrace(); }
//长度 System.out.println(f.length()); }
|
|
2.InputStream/OutputStream
文件:FileInputStream/FileOutputStream
2.1 FileInputStream(输入流)
数据从文件到java代码中。
int read(); //读取一个字节
int read(byte[]); //读取一串字节
long avaliable; //文件长度
2.2 FileInputStream(字节文件输入流)
new FileInputStream(File);
new FileInputStream("文件路径+文件名");
| |
|
|
public static void main(String[] args) { //文件内容 File f=new File("src/a.txt"); try { //英文和中午翻译 //码表 InputStream is=new FileInputStream(f); int b=is.read(); //1:1的unicode编码 System.out.println((char)b); b=is.read(); System.out.println((char)b); is.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
|
|
2.3 FileOutputStream(输出流)
数据从java代码中,写到文件或者其他介质中。
void write(字节); //写入一个字节
void write(byte[]); //写入字节数组
2.4 FileOutputStream
new FileOutputStream(File);
new FileOutputStream("文件路径+文件名");
new FileOutputStream("文件路径+文件名",true);
注意:a.boolean:表示是否向文件末尾追加,如果是true,表示追加,false表示不追加(也就是覆盖),默认值为false
b.创建FileOutputStream实例时,如果相应的文件并不存在,则会自动创建一个空的文件。
| |
|
|
public static void main(String[] args) throws Exception{ File f=new File("src/a.txt"); OutputStream os=new FileOutputStream(f); os.write(97); os.close(); }
|
|
3.Reader/Writer(字符流)
3.1 FileReader
bw.write(字符串);
注意:a.能够用文本编辑器打开的文件,不乱码就是字符文件
b.能够用文本编辑器打开乱码的,就是字节文件
|
public class TestChar { public static void main(String[] args) throws Exception { // read(); write(); }
/** * 字符输出流Writer * @throws IOException */ private static void write() throws IOException { Writer w=new FileWriter("src/a.txt"); char[] cs={'钓','鱼','岛','是','中','国','的'}; w.write(cs); w.close(); }
/** * 字符输入流Reader * @throws Exception */ private static void read() throws Exception{ Reader r=new FileReader("src/d.txt"); // int b=r.read(); // System.out.println((char)b);
char[] chars=new char[1024]; int length=r.read(chars); System.out.println(Arrays.toString(chars)); } }
|
|
|
public class TestBuffer { public static void main(String[] args) throws Exception { buReader(); buWriter();
}
private static void buWriter() throws Exception { BufferedWriter bw=new BufferedWriter(new FileWriter("src/a.txt")); bw.write("我是中国人"); //刷新缓存 bw.flush(); //默认执行flush(),关闭管道 bw.close(); }
private static void buReader() throws Exception { BufferedReader br=new BufferedReader(new FileReader("src/d.txt")); //读取一行记录 // String str=br.readLine(); // str=br.readLine(); // System.out.println(str);
String str; while((str=br.readLine())!=null){ System.out.println(str); } }
}
|
|