Java implements I and o stream code sharing

  • 2020-04-01 04:12:16
  • OfStack

1, write a program, read the contents of the file test.txt and output in the console. If the source file does not exist, the corresponding error message is displayed.


 package src;
 
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
 
 public class Test {
 
   public static void main(String[] args) {
     File f = new File("src\test.txt");//The file is in SRC named test.txt
     try {
       FileReader fr = new FileReader(f);//Reads the file into the content
       int m;
       while((m=fr.read())!=-){
         System.out.print((char)(m));
       }
     } catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   }
 }

2. Write a program to read 80 bytes from the file fin.txt in the current directory (the actual number of bytes read may be less than 80) and write the bytes read to the file fout.txt in the current directory


 package src;
 
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 
 public class Test {
 
   public static void main(String[] args) {
     File f = new File("src\fin.txt");//SRC under the file fin.txt
     File f = new File("src\fout.txt");//SRC under fout.txt file
     
     try {
       FileInputStream fis = new FileInputStream(f);
       FileOutputStream fos = new FileOutputStream(f);
       
       byte[] temp = new byte[];//Define a byte array
       fis.read(temp);//Read into memory
       fos.write(temp);//Written to a file
       
       fis.close();
       fos.close();
     } catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
     
   }
 }

3, use Java's input/output stream technology to read the contents of a text file in lines, with line Numbers added sequentially for each line read and written to another file.


 package src;
 
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 
 public class Test {
 
   public static void main(String[] args) {
     File f = new File("src\fin.txt");//SRC under the file fin.txt
     File f = new File("src\fout.txt");//SRC under fout.txt file
     
     try {
       FileReader fr = new FileReader(f);
       FileWriter fw = new FileWriter(f);
       
       BufferedReader br = new BufferedReader(fr);
       BufferedWriter bw = new BufferedWriter(fw);
       
       String temp;
       int i=;
       while((temp=br.readLine())!=null){
         bw.write(i+","+temp);
         bw.newLine();//A newline
         i++;
       }
       bw.flush();//Write the contents of the buffer to the file
       br.close();
       bw.close();
       br.close();
       bw.close();
     } catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   }
 }

4, write a program, receive from the keyboard input data, and input from the keyboard content written to input.txt file, if the input "quit", the program ends.


 package src;
 
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.util.Scanner;
 
 public class Test {
 
   public static void main(String[] args) {
     File f = new File("src\input.txt");
     try {
       FileWriter fw = new FileWriter(f);
       Scanner scanner = new Scanner(System.in);
       String temp;
       while(!((temp=scanner.nextLine()).equals("quit"))){
         fw.write(temp);
       }
       fw.close();
     } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   }
 }

5, write a program to achieve the following functions, file fin.txt is a Chinese file with no line structure (no line break), read characters from fin, write to the file fou.txt, every 40 characters one line (the last line may be less than 40 words)


 package src;
 
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 public class Test {
 
   public static void main(String[] args) {
     File f=new File("src\fin.txt");
     File f=new File("src\fout.txt");
     try {
       FileReader fr=new FileReader(f);
       FileWriter fw=new FileWriter(f);
       
       char temp[]=new char[];
       int len;
       while((len=fr.read(temp))!=-)
       {
         if(len==)
          fw.write(new String(temp)+"n");
         else
           fw.write(temp, , len);
       }
       fr.close();
       fw.close();
       
     } catch (FileNotFoundException e) {
       //TODO automatically generates a catch block
       e.printStackTrace();
     } catch (IOException e) {
       //TODO automatically generates a catch block
       e.printStackTrace();
     }
   }
 }


Related articles: