The Java file manipulation exercise code reads the file under a disk character

  • 2020-04-01 02:48:27
  • OfStack


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
public class IORead {
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  try {
   //Methods a
   BufferedReader br = new BufferedReader(new FileReader(new File(
   "D:/project/transfar/doc/1.txt")));
   // StringBuilder bd = new StringBuilder();
   StringBuffer bd = new StringBuffer();
   while (true) {
    String str = br.readLine();
    if (str == null) {
     break;
    }
    System.out.println(str);
    bd.append(str);
   }
   br.close();
   // System.out.println(bd.toString());
   //Method 2
   InputStream is = new FileInputStream(new File("D:/project/transfar/doc/1.txt"));
   byte b[] = new byte[Integer.parseInt(new File("D:/project/transfar/doc/1.txt").length()
   + "")];
   is.read(b);
   System.out.write(b);
   System.out.println();
   is.close();
   //Methods three
   Reader r = new FileReader(new File("D:/project/transfar/doc/1.txt"));
   char c[] = new char[(int) new File("D:/project/transfar/doc/1.txt").length()];
   r.read(c);
   String str = new String(c);
   System.out.print(str);
   r.close();
  } catch (RuntimeException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}


Related articles: