zoukankan      html  css  js  c++  java
  • Java基础——I/O

    文本I/O与二进制I/O   

      在计算机中所有的文件都是以二进制的形式来存储的,所以本质上所有的文件都是二进制文件。
      文本I/O建立在二进制I/O的基础之上,它能提供字符层次的编码和解码的抽象,在写入一个字符时,Java虚拟机会将统一码转化为文件指定的编码,读文件同理。而二进制I/O不需要转化。如果使用二进制I/O向文件写入一个数值,就是将内存中的确切值复制到文件中。
      由于二进制I/O不需要编码和解码,所以,二进制I/O比文本I/O效率高
      二进制文件与主机的编码方案无关,因此,它是可移植的。在任何机器上的Java程序可以读取Java程序所创建的二进制文件。

    Java 文本I/O

      java.io.File file = new java.io.File("hello.java");
        文件和目录路径名的抽象表示形式。
        File 类的实例是不可变的;也就是说,一旦创建,File 对象表示的抽象路径名将永不改变。
          ->为在当前目录下的文件hello.txt创建一个File对象
          ->也可以是明确的绝对路径,如"/Users/zhengbinMac/Workspaces/hello.java"
          ->在Windows中目录的分隔符是反斜杠(),但是在Java中,反斜杠是一个特殊字符,所以应该写为\。

      java.io.PrintWriter = new java.io.PrintWriter("File file");
        使用指定文件创建不具有自动行刷新的新 PrintWriter。
        可用于创建一个文件并向文本文件写入数据。
        会报出FileNotFoundException异常。

      java.util.Scanner s = new java.util.Scanner(File);
        创建一个所产生的值都是从特定文件扫描而来的扫描器
      java.util.Scanner s = new java.util.Scanner(String);
        创建一个所产生的值都是从特定字符串扫描而来的扫描器

     1 public class ReplaceDate{
     2     public static void main(String args[]) {
     3         try{
     4             java.io.File f1 = new java.io.File("/Users/zhengbinMac/Workspaces/Revise Java/Files and I:O/123.txt");
     5             System.out.println(f1.exists());
     6             java.util.Scanner input = new java.util.Scanner(f1);
     7             java.io.File f2 = new java.io.File("/Users/zhengbinMac/Workspaces/Revise Java/Files and I:O/321.txt");
     8             System.out.println(f2.getAbsolutePath());
     9             java.io.PrintWriter output = new java.io.PrintWriter(f2);
    10             String s1 = new String();
    11             if(input.hasNext()){
    12                 s1 = input.nextLine();
    13                 String s2 = s1.replaceAll("Hello","OK1");
    14                 output.println(s2);
    15             }
    16             input.close();
    17             output.close();
    18         }catch(java.io.FileNotFoundException e){
    19             e.getStackTrace();
    20         }
    21     }
    22 }
    ReplaceDate.java

    Java 二进制I/O

      字节流
        Java字节流是用来处理8比特字节的输入和输出。
        java.io.FileInputStream(File/String)
        java.io.FileOutputStream(File/String)

     1 public class CopyFile {
     2             public static void main(String[] args) throws java.io.IOException {
     3                 java.io.FileInputStream in = null;
     4                 java.io.FileOutputStream out = null;
     5                 try{
     6                     in = new java.io.FileInputStream("input.txt");
     7                     out = new java.io.FileOutputStream("output.txt");
     8                     int c;
     9                     while((c = in.read()) != -1){
    10                         out.write(c);
    11                     }
    12                 }catch(java.io.IOException e){
    13                     e.getMessage();
    14                 }finally{
    15                     if(in != null){
    16                         in.close();
    17                     }
    18                     if(out != null){
    19                         out.close();
    20                     }
    21                 }
    22             }
    23         
    CopyFile.java

      

      字符流
        Java字节流是用来处理8比特字节的输入和输出,而字符流用于处理16位,即FileReader与FileWriter一次读写两个字节。

      文件输入流
        InputStream f = new FileInputStream("/Users/zhengbinMac/Workspaces");
          或
        File f = new File("/Users/zhengbinMac/Workspaces");
        InputStream f = new FileInputStream(f);

      文件输出流
        OutputStream f = new FileOutputStream("/Users/zhengbinMac/Workspaces");
          或
        File f = new File("/Users/zhengbinMac/Workspaces");
        OutputStream f = new FileOutputStream(f);

     1 import java.io.*;
     2         public class fileStreamTest{
     3            public static void main(String args[]){
     4                try{
     5                   byte bWrite [] = {11,21,3,40,5};
     6                   OutputStream os = new FileOutputStream("test.txt");
     7                   for(int x=0; x < bWrite.length ; x++){
     8                      os.write( bWrite[x] ); // writes the bytes
     9                   }
    10                   os.close();
    11 
    12                   InputStream is = new FileInputStream("test.txt");
    13                   int size = is.available();
    14 
    15                   for(int i=0; i< size; i++){
    16                      System.out.print((char)is.read() + "  ");
    17                   }
    18                   is.close();
    19                }catch(IOException e){
    20                   System.out.print("Exception");
    21                }    
    22            }
    23         }
    fileStreamTest.java

    扩展:

      1.续写文件,不覆盖原有内容

    1   public static void fileLogger(String out) throws FileNotFoundException {
    2         File file = new File(path);
    3         if(file.exists()){
    4             file.mkdirs();
    5         }
    6         PrintStream p = new PrintStream(new BufferedOutputStream(new FileOutputStream(file, true)));
    7         p.println(out);
    8         p.close();
    9     }
  • 相关阅读:
    HUST 1372 marshmallow
    HUST 1371 Emergency relief
    CodeForces 629D Babaei and Birthday Cake
    CodeForces 629C Famil Door and Brackets
    ZOJ 3872 Beauty of Array
    ZOJ 3870 Team Formation
    HDU 5631 Rikka with Graph
    HDU 5630 Rikka with Chess
    CodeForces 626D Jerry's Protest
    【POJ 1964】 City Game
  • 原文地址:https://www.cnblogs.com/zhengbin/p/5184271.html
Copyright © 2011-2022 走看看