当前位置:

Java文件流操作

访客 2024-04-25 792 0

一、文件创建和删除

   public static void main(String[] args) throws IOException {File file = new File("..\\hello-world.txt");//..表示在上机目录下创建hello-world.txtSystem.out.println(file.getPath());//返回当前相对路径System.out.println(file.getCanonicalFile());//返回绝对路径System.out.println(file.exists());//false//创建文件boolean re=file.createNewFile();System.out.println(re);System.out.println(file.exists());//true//删除文件System.out.println(file.exists());//trueboolean se=file.delete();System.out.println(file.exists());//false}

二、文件读写分类(重点讨论)

文件大多数是二进制保存的,当然也有以保存字符的文本文件,当我们读写文件的时候,我们需要针对这两种文件类型有不同操作,针对二进制的文件我们叫字节流,也就是每次读写一个字节,针对文本文件我们读写操作叫字符流,每次读取多个字节,这里多个字节没有准确几个字节,GBK每个中文字符有两个字节,UTF8每个中文字符是三个字节

 三、测试环境

 描述:Test目录下创建了两个目录,aaa和bbb,这个两个目录下分别创建文件111和222

四、字符流操作

 1、读操作

··方法一 

public class Test_Read {public static void main(String[] args) throws IOException {Reader reader=new FileReader("e:/Test/aaa/111.txt");while (true){int x=reader.read();if(x==-1){//读取完毕break;}char y=(char)x;//一个中文字符一个中文字符读取System.out.print(y+" ");}}}

描述:用read方法一个中文字符一个中文字符读取但是read方法返回的是一个整数,如果是-1说明为文件空读取结束,不为空传为字符输出。 

 ··方法二

public class Test_Read_Array {public static void main(String[] args) throws IOException {Reader reader=new FileReader("e:/Test/aaa/111.txt");while (true){char[] array=new char[2];int n=reader.read(array);//每次只读取两个中文字符,并且打印,多次读取if(n==-1){//读取完毕break;}System.out.println("n="+n);for (int i=0;i<n;i++) {System.out.print(array[i]);}System.out.println();}}}

 文件资源泄露问题(重点)

你有没有发现上述代码存在致命问题!!!,在while循环里面我们频繁的下创建reader对象,这会填满文件资源表,文件资源表一旦填满那么我们在想操作该文件将会失败,所以我们应该及时关闭我们创建的对象,以读取对象为例修改:

public class Test_Read_Array {public static void main(String[] args) throws IOException {//try()中的资源会随着try结束而释放try ( Reader reader=new FileReader("e:/Test/aaa/111.txt");){while (true){char[] array=new char[2];int n=reader.read(array);//每次只读取两个中文字符,并且打印,多次读取if(n==-1){//读取完毕break;}System.out.println("n="+n);for (int i=0;i<n;i++) {System.out.print(array[i]);}System.out.println();}}}}

这只是一种简单写法,当然你可以在try后面用finally配合read.close(),同样可以 ,我们下面的代码将会采用这种简便的写法

2、写操作

public class Test_Write {public static void main(String[] args) {//文件路径后面不加true默认是false会覆盖原来文件内容,加了true是在原来文件内容后添加新内容try(Writer writer=new FileWriter("e:/Test/aaa/111.txt",true)){//写入数据writer.write("我看到了");} catch (IOException e) {throw new RuntimeException(e);}}}

五、字节流操作 

1、读操作

public class Test_Read {//字节流读操作public static void main(String[] args) throws IOException {try(InputStream inputStream=new FileInputStream("E:/Test/bbb/222.txt")){//开始读取文件Scanner scanner=new Scanner(inputStream);String s=scanner.next();System.out.println(s);}}}

2、写操作 

public class Test_Write {public static void main(String[] args) throws FileNotFoundException {try(OutputStream outputStream=new FileOutputStream("E:/Test/bbb/222.txt")){PrintWriter writer=new  PrintWriter(outputStream);writer.printf("我知道了");} catch (IOException e) {throw new RuntimeException(e);}}}

 

啊?非但没有写入而且原来的数据害消失了!!! 

这是因为缓冲区问题,所谓缓冲区问题就是,我们这个写的操作实际上是先写入缓存区,缓存区的数据到达一定数量后在写进硬盘,所以我们现在要直接把缓冲区的内容写进硬盘就可以了

public class Test_Write {public static void main(String[] args) throws FileNotFoundException {try(OutputStream outputStream=new FileOutputStream("E:/Test/bbb/222.txt")){PrintWriter writer=new  PrintWriter(outputStream);writer.printf("我知道了",true);writer.flush();} catch (IOException e) {throw new RuntimeException(e);}}}

发表评论

  • 评论列表
还没有人评论,快来抢沙发吧~