用FileInputStream和FileOutPutStream读写文件

来源:互联网 发布:java遍历json对象 编辑:程序博客网 时间:2024/05/21 17:25



用FileInputStream和FileOutPutStream读写文件

 


用FileInputStream和FileOutPutStream读写文件

package io;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class TestIO {
public static void main(String[] args) throws IOException {
   //写文件
   File file=new File("demo1.txt");//抽象一个1.txt的文件,处理文件名或者路径
   //构造两个FileOutPutStream的实例对象,先定义一个File类文件的做法比较规范,通用
   FileOutputStream fos1=new FileOutputStream(file,false);//true表示如果文件已经存在,没执行程序一次便往文件追加一次内容,否则会每一次的执行结果会覆盖文件上一次执行结果
   BufferedOutputStream bos=new BufferedOutputStream(fos1);//把fos1封装成具有缓冲功能的文件输出流
   FileOutputStream fos2=new FileOutputStream("demo2.txt");//用一个String构造一个FileOutputStream对象,String为文件名,这种构造方法也可以有两个参数,同上
   DataOutputStream dos=new DataOutputStream(fos2);//DataOutputStream提供了写入任意对象的能力
   String s1="How are you?";
   String s2="Fine.Thanks!";
   dos.writeBytes(s1+"   "+s2);
   fos1.write(s1.getBytes());//把字符串s1以字节流形式写入1.txt文件中
//   fos2.write(s2.getBytes());//把字符串s2以字节形式流写入2.txt文件中
   fos1.write(s2.getBytes());

  
  
   //读出文件内容
   FileInputStream fis=new FileInputStream(file);
   byte[] b=new byte[fis.available()];//创建一个字节数组,数组长度与file中获得的字节数相等
   while(fis.read(b)!=-1){
    System.out.println(new String(b));//打印出从file文件读取的内容
   }
  
   //复制文件
   //用“.”把file的文件名分隔开
   String[]s=file.getName().split("\\.");//因为"."在正则表达式中有特殊的含义,使用的时候必须进行转义。
   String copyFName=s[0]+"_"+"copy"+"."+s[1];
   File file1=new File(copyFName);
   FileOutputStream fos=new FileOutputStream(file1,true);
   fos.write(b);//把刚刚从file 文件读出的内容写入副本file1
  
}

}

结果:

打印结果:How are you?Fine.Thanks!

运行结果:程序执行后,在该工程下生成了demo1.txt,demo2.txt,demo1_copy.txt三个文件其中的内容分别为:“How are you?Fine.Thanks! ”,“How are you?   Fine.Thanks!”,"How are you?Fine.Thanks!"

总结:

1、FileInputStream是InputStream类(字节流输入)的子类,以字节流方式读取文件;FileInputStream的构造函数参数可以是File文件,也可以是字符串,但实际上使用File文件会更加规范。

2、类似的,FileOutputStream是OutputStream类的子类,以字节流方式写入文件。

3、与File类不同(File类关注的是文件在磁盘上的存储),FileInputStream,FileOutputStream关注是的文件的内容




 在使用fileoutputstream时经常出现FileNotFoundException问题,即便是同一个程序(可行)改了一下包名再重新编译,就会无缘无故的抛出FileNotFoundException问题。这曾经困扰我好几个月,前几次都稀里糊涂的解决了,今天又出问题了,便下定决心解决了它。

        首先,要明确FileOutputStream并不会帮你创建不存在的路径,所以要先创建路径,再创建文件。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span>    </span>File cacheDir = new File("data/data/com.kavinapps.androidk.pcexam/databases/");//设置目录参数  
  2.     cacheDir.mkdirs();//新建目录  
  3.     Log.i("copySd2phone","新建data/data目录成功");  
  4.     String filename;  
  5.     //获得文件名的长度  
  6.     filename = "carnum.db";   
  7.     Log.i("createNewFile","filename= "+filename);  
  8.     //文件名  
  9.     cacheFile = new File(cacheDir,filename);//设置参数  
  10.     cacheFile.createNewFile();//生成文件  
  11.     Log.i("createNewFile","生成文件成功"+cacheFile.getName());  
  12.               
  13.         output = new FileOutputStream( "data/data/com.kavinapps.androidk.pcexam/databases/carnum.db" );  
        在以前,我往往只使用最后一行,有时候不会出错,但大多数时候行不通。

0 0
原创粉丝点击