1. Write a program that reads the contents of the file test.txt and outputs it 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 T est { public static void main(String[] args) { File f = new File("src//test.txt");//The file is in the src named test.txt try { FileReader fr = new FileReader(f);//Read 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(); } c atch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }2. Write a program to implement the following function, read 80 bytes from the file fin.txt in the current directory (the actual number of bytes may be less than 80) and write the read bytes to the current directory. file fout.txt
package src; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import j ava.io.IOException; public class Test { public static void main(String[] args) { File f = new File("src//fin.txt");// fin.txt file under src File f = new File("src//fout.txt");// src fout.txt File try { FileInputStream fis = new FileInputStream(f); FileOutputStream fos = new FileOutputStream(f); byte[] temp = new byte[];//Define a byte array fis.read(te mp);//Read the memory fos.write(temp);//Write to 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 streaming technology to read the content of a text file by line. Each line is read out, the line numbers are added in sequence and written to another file.
package src; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import ja va.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"); // fin.txt file under src File f = new File("src/ /fout.txt");//src 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();//Break i++; } bw.flush();//Write the buffer content 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 that receives the data entered from the keyboard and writes the content entered from the keyboard into the input.txt file. If "quit" is entered, 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 st atic 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.nextLin e()) .equals("quit"))){ fw.write(temp); } fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }5. Write a program to implement the following functions. The file fin.txt is a Chinese file with no line structure (no newline characters). It reads characters from fin and writes them to the file fou.txt, with one line for every 40 characters (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.I OException; 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 generated catch block e.printStackTrace( ); } catch (IOException e) { // TODO automatically generated catch block e.printStackTrace(); } } }