JAVA中纯文本的读与写、拷贝

来源:互联网 发布:淘宝上的正品足球鞋店 编辑:程序博客网 时间:2024/05/22 05:32

字符流:只能处理纯文本、全部为可见字符、txt、html

节点流:Reader FileReader、Writer  FileWriter

读取纯文本步骤与代码:

public class ReadDemo {       /**        * 1.创建源   File对象        * 2.选择流   Reader FileReader        * 3.操作            read()        * 4.释放资源 关闭流        */       public static void main(String[] args){              File src = new File("C:/Users/Administrator/Desktop/android学习笔记/a.txt");              Reader reader = null;              try {                     reader = new FileReader(src);                     char[] test = new char[1024];                     int len = 0;                     while(-1!=(len=reader.read(test))){                           String string = new String(test, 0, len);                           System.out.println(string);                     }              } catch (FileNotFoundException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();                     System.out.println("文件不存在");              } catch (IOException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();                     System.out.println("读取文件失败");              }finally {                     if(null!=reader){                           try {                                  reader.close();                           } catch (IOException e) {                                  // TODO Auto-generated catch block                                  e.printStackTrace();                                  System.out.println("关闭文件失败");                           }                     }              }       }}


纯文本的写出:

public class WriteDemo {       /**        * 1.创建源   File对象        * 2.选择流   Writer FileWriter        * 3.操作            Write(str) ; append(char c);        * 4.释放资源 关闭流        */       public static void main(String[] args){              File dest = new File("C:/Users/Administrator/Desktop/android学习笔记/a_write.txt");              Writer writer = null;              try {                     writer = new FileWriter(dest);                     String string = new String("Hello!Writer");                     writer.write(string);                     writer.append("测试测试");                     writer.flush();              } catch (FileNotFoundException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();              } catch (IOException e) {                     // TODO: handle exception                     e.printStackTrace();              }finally {                     if(null!=writer){                           try {                                  writer.close();                           } catch (IOException e) {                                  // TODO Auto-generated catch block                                  e.printStackTrace();                           }                     }              }       }}


纯文本的拷贝:

public class CopyDemo {       /**        * 纯文本的拷贝        * 1.建立联系        File对象        * 2.选择流   Reader FileReader   Writer FileWriter        * 3.操作            char[] test = new char[20];        *                   定义每次实际读的长度;        *                   while语句循环读取完文件数据{        *                         writer.write();        *                         writer.flush();        *                   }        * 4.释放资源 关闭流        */       public static void main(String[] args){              File src = new File("C:/Users/Administrator/Desktop/android学习笔记/a.txt");              File dest = new File("C:/Users/Administrator/Desktop/android学习笔记/a_copy.txt");              Reader reader = null;              Writer writer = null;              try {              reader = new FileReader(src);              writer = new FileWriter(dest);              char[] test = new char[1024];              int len =0;              while(-1!=(len=reader.read(test))){                           writer.write(test, 0, len);                           writer.flush();                     }              } catch (FileNotFoundException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();              }catch (IOException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();       }finally {              try {                     writer.close();              } catch (IOException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();              }              try {                     reader.close();              } catch (IOException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();              }       }}}




0 0
原创粉丝点击