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 appropriate 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");/file in src name test.txt Medium
try {
FileReader fr = new FileReader (f);//Read the file to 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 achieve the following functions, from the current directory in the file Fin.txt read 80 bytes (actually read the number of bytes may be less than 80) and the read bytes are written to the current directory in the file Fout.txt
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 fin.txt files
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[];//defines a byte array
fis.read (temp);//read in 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, using Java input/output streaming technology to read the contents of a text file by line, add the line number in sequence and write to another file in each row.
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 Fin.txt file
File F = new file ("Src\\fout.txt");//src fout.txt files 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 ();//newline i++;
Bw.flush ()//write the buffer contents to the file Br.close ();
Bw.close ();
Br.close ();
Bw.close (); The catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
}
}
}
4, write a program to receive data from the keyboard input, and the input from the keyboard to the Input.txt file, if you enter "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 no line structure (no line character) of the Chinese file, read characters from the fin, write to file fou.txt, every 40 characters one line (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 generated catch block
e.printstacktrace ();
} catch (IOException e) {
//TODO automatically generated catch blocks
e.printstacktrace ();}
}