首页 > 代码库 > java1.7与Android中的文件I/O操作(草稿)

java1.7与Android中的文件I/O操作(草稿)

说明:文件I/O的关键类比较多,这里总结一下。

参考:《Java 7 入门经典》

1、Java输入流


图1,java常用输入输出流的结构图(第一版)

 

 

1)标准流

标准流

类型

方法

System.in

InputStream

 

System.out

PrintStream

例如:printf(“b=%2$-15.2f”,a,b)

System.err


2)字节型

典型方式-1:从文件中读取

 File file = ...
   InputStream in = null;
   try {
     in = new BufferedInputStream(new FileInputStream(file));
     ...
    finally {
     if (in != null) {
       in.close();
     }
   }
 }

典型方式-2:从文件中读取(java1.7

Files.newInputStream(Path file)
Path file = Path.get(“D:/cao.java”)
   InputStream in = null;
   try {
    in = new BufferedIntputStream(Files.newInputStream(file));
     ...
    finally {
     if (out != null) {
       out.close();
     }
   }
 }
典型方式-3从控制台读取
in = new BufferedInputStream(System.in);

3)字符型

典型方式-1:从文件中读取***不太好***

BufferedReader buf=new BufferedReader(new FileReader(String file))//不能编码
BufferedReader buf = new BufferedReader(new FileReader("file.java"));

典型方式-2:从文件中读取***较好***

BufferedReader buf=new BufferedReader(new InputStreamReader(new FileInputStream(String file), String charsetName))  //可以编码
InputStreamReader(InputStream in, String charsetName)
 File file = ...
 ...
BufferedReader buf = new BufferedReader(new InputStreamReader(new FileInputStream(String file), “utf-8”);

典型方式-3:从文件中读取***java1.7最佳**

InputStream newInputStream(Path path,OpenOption... options)

Path file=Paths.get();
…
BufferedInputStream in=new BufferedInputStream(Files.newInputStream(file, Charset.forName(“UTF-8”)))

4)通道输入流

(-- 待完成 --)

2、Java输出流


1)字节型

典型方式-1:打开文件的输出流

FileOutputStream (File file,boolean append),append为true追加模式,没有则新建

 File file = ...
   OutputStream out = null;
   try {
    out = new BufferedOutputStream(new FileOutputStream(file));
     ...
    finally {
     if (out != null) {
       out.close();
     }
   }
 }

典型方式-2:打开文件的输出流(java1.7)

Files.newOutputStream(Pathfile,CREATE,APPEND),没有则新建,追加模式

Path file = Path.get(“D:/caoyanfeng.java”)
   OutputStream out = null;
   try {
    out = new BufferedOutputStream(Files.newOutputStream(file));
     ...
    finally {
     if (out != null) {
       out.close();
     }
   }
 }

2)字符型

典型方式-1:打开文件的输出流(***不好的方式***)

FileWriter(File file,boolean append),append为true追加模式,没有则新建。不能指定编码方式。

File file = ...
   Writer writer = null;
   try {
    writer = new BufferedWriter(new FileWriter(file));
     ...
    finally {
     if (writer != null) {
       writer.close();
     }
   }
 }

 典型方式-2:打开文件的输出流***较好方式***

OutputStreamWriter(new FileOutputStream(File file,  booleanappend),"UTF-8") , append为true追加模式,没有则新建,utf-8编码

File file = ...
  Writer writer = null;
   try {
    writer = new OutputStreamWriter(new FileOutputStream(file),”UTF-8”);
     ...
   writer.write("曹艳丰");
   writer.flush;
    finally {
     if (writer != null) {
       writer.close();
     }
   }
 }

典型方式-3:打开文件的输出流***最佳方式***

Files.newBufferedWriter(Path file,Charset.forName(“UTF-8”), CREATE,APPEND),没有则新建,追加模式,utf-8编码

Path file = Path.get(“D:/cao.java”)
  Writer writer = null;
   try {
    writer = new BufferedWriter(Files. newBufferedWriter (Path file,Charset.forName(“UTF-8”), CREATE,APPEND));
     ...
    finally {
     if (writer != null) {
       writer.close();
     }
   }
 }

3)通道输出流

(-- 待完成 --)

3、Android输入流

Android 的输入输出流基本和java相同,只是路径需要使用手机路径。另外,context提供了操作程序路径下的两个字节流。

FileInputStream openFileInput (String name)

FileOutputStream openFileOutput (String name,int mode)

1)字节型

获得文件的FileInputStream,然后进行封装

BufferedInputStream  in = new BufferedInputStream(new FileInputStream(File file));

典型例子-1:操作本程序data文件夹的IO

Android的context方法

FileInputStream openFileInput (String name)

典型例子-2:指定路径下的IO(同java)

File file=new File(dir,"/data/com.iteye.androidtoast/androidtoast.txt");
BufferedInputStream  in = new BufferedInputStream(new FileInputStream(File file));

2)字符型

3、 Android输出流

1)字节型

获得文件的FileOutputStream,然后进行封装

再将FileOutputStreamfos进行封装进PrintStream 或BufferedOutputStream

BufferedOutputStream out=new  BufferedOutputStream(openFileInput (String name)

PrintStream out=new BufferedOutputStream(openFileInput (String name))

典型例子-1:操作本程序data文件夹的IO

 FileOutputStream openFileOutput (String name, int mode)
即 FileOutputStream fos= openFileInput (String name)

典型例子-2:操作指定文件夹的下的IO

FileOutputStream(File file, boolean append),与java字节流相同

File dir = Environment.getDataDirectory();//得到data目录   
File file=new File(dir,"/data/com.iteye.androidtoast/androidtoast.txt");   
FileOutputStream out=new FileOutputStream(file, true);  

2)字符型

典型例子-1:打开文件的输出流(***java,不好的方式***)

File dir = Environment.getDataDirectory();//得到data目录 File file=new File(dir,"/data/com.iteye.androidtoast/androidtoast.txt");   
FileWriter fw=new FileWriter(file,true);   

到这里可以直接写入文件,也可以进行再次封装

BufferedWriter bw=new BufferedWriter(new FileWriter(file,true));

典型方式-2:打开文件的输出流***java,较好方式***

File file=new File(dir,"/data/com.iteye.androidtoast/androidtoast.txt");
OutputStreamWriter(new FileOutputStream(File file,  boolean append),"UTF-8") // append为true追加模式,没有则新建,utf-8编码

























java1.7与Android中的文件I/O操作(草稿)