IO basic usage in Java

First posted what I used in the job three file input auxiliary class, three file output auxiliary class

public class BuffIn implements InHelp{
  private BufferedReader bufferedReader;

  public BuffIn(String path) {
    try {
      bufferedReader = new BufferedReader(
              new InputStreamReader(new FileInputStream(path), "UTF-8"));
    }catch (IOException e){
      System.err.println ( "Please provide the correct path" );
      E2 PathException = new new PathException (path, "Error path address" );
      Logs.logger.error(e2.getMessage());
    }
  }

  @Override
  public String read() throws IOException {
    TimeCounter.start();
    String s = bufferedReader.readLine();
    TimeCounter.pause();
    return s;
  }

  @Override
  public void shut() throws IOException {
    bufferedReader.close();
  }
}

public class BuffreaderIn implements InHelp{
  private BufferedReader bufferedReader;
  public BuffreaderIn(String path) {
    try {
      bufferedReader = new BufferedReader(new FileReader(path));
    }catch (IOException e){
      System.err.println ( "Please provide the correct path" );
      E2 PathException = new new PathException (path, "Error path address" );
      Logs.logger.error(e2.getMessage());
    }
  }

  @Override
  public String read() throws IOException {
    TimeCounter.start();
    String s = bufferedReader.readLine();
    TimeCounter.pause();
    return s;
  }


  @Override
  public void shut() throws IOException {
    bufferedReader.close();
  }
}

public class ScannerIn implements InHelp{
  private Scanner scanner;
  private java.io.File file;
  public ScannerIn(String path) {
    try {
      file = new java.io.File(path);
      scanner = new Scanner(file);
    }catch (IOException e){
      System.err.println ( "Please provide the correct path" );
      E2 exception.PathException = new new exception.PathException (path, "Error path address" );
      logs.Logs.logger.error(e2.getMessage());
    }
  }

  @Override
  public String read() throws IOException {
    TimeCounter.start();
    if(!scanner.hasNextLine()) return null;
    String s = scanner.nextLine();
    TimeCounter.pause();
    return s;
  }

  @Override
  public void shut() throws IOException {
    scanner.close();
  }
}
public class BuffOut implements OutHelp{
  private BufferedWriter bufferedWriter;
  private FileOutputStream fileOutputStream;
  public BuffOut(String path) {
    try {
      fileOutputStream = new FileOutputStream(path);
      bufferedWriter = new BufferedWriter(
              new OutputStreamWriter(fileOutputStream, "UTF-8"));
    }catch (IOException e){
      System.err.println ( "Please provide the correct path" );
      E2 PathException = new new PathException (path, "Error path address" );
      Logs.logger.error(e2.getMessage());
    }
  }

  @Override
  public void write(String s) throws IOException {
    TimeCounter.start();
    bufferedWriter.write(s);
    TimeCounter.pause();
  }

  @Override
  public void shut() throws IOException {
    bufferedWriter.close();
    fileOutputStream.close();
  }
}

public class StreamOut implements OutHelp{
  private OutputStreamWriter outputStreamWriter;
  private FileOutputStream fileOutputStream;

  public StreamOut(String path) {
    try {
      fileOutputStream = new FileOutputStream(path);
      outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
    }catch (IOException e){
      System.err.println ( "Please provide the correct path" );
      E2 PathException = new new PathException (path, "Error path address" );
      Logs.logger.error(e2.getMessage());
    }
  }

  @Override
  public void write(String s) throws IOException {
    TimeCounter.start();
    outputStreamWriter.write(s);
    TimeCounter.pause();
  }

  @Override
  public void shut() throws IOException {
    outputStreamWriter.close();
    fileOutputStream.close();
  }
}

public class WriterOut implements OutHelp {
  private FileWriter fileWriter;

  public WriterOut(String path) {
    try {
      fileWriter = new FileWriter(path);
    }catch (IOException e){
      System.err.println ( "Please provide the correct path" );
      E2 PathException = new new PathException (path, "Error path address" );
      Logs.logger.error(e2.getMessage());
    }
  }

  @Override
  public void write(String s) throws IOException {
    TimeCounter.start();
    fileWriter.write(s);
    TimeCounter.pause();
  }

  @Override
  public void shut() throws IOException {
    fileWriter.close();
  }
}

 

 The main classes are as follows:

     1. File (File Characteristics and Management): information describing a file or directory, for example, generates a new directory, modify the file name, delete the file, determines the path files.

     2. InputStream (Operation binary format): abstract class, based on the input operation byte is the parent class for all input streams. Defines the common feature of all input streams have.

     3. OutputStream (Operation binary format): abstract class. Byte output operation based. It is the parent class of all output streams. It defines a common feature of all the output streams have.

     4.Reader (Operation File Format): abstract class, a character based on the input operation.

     5. Writer (Operation File Format): abstract class, based on the operation of the output character.

     6. RandomAccessFile (random file operations): a separate class, it inherits directly to the Object rich features, can be accessed (O) operated from anywhere in the file.

For some of the frequent interaction devices, Java language system booked three stream object can be used directly, namely:

· System.in (standard input), typically represents keyboard input.

· System.out (stdout): usually written to the display.

· System.err (standard error): usually written to the display.

 Class subclasses the IOException
    1.public class EOFException: abnormal end of file or end of the input stream, this type of exception is thrown.    

      2.public class FileNotFoundException: When the file is not found, throw an exception.

      3.public class InterruptedIOException: When the I / O operation is interrupted, this type of exception thrown.


 

Guess you like

Origin www.cnblogs.com/hyfer/p/11079921.html