For example: Write a text file, create a text file Hello.txt in the D disk directory, and write several lines of text into it.
importjava.io.*;
/*
* Writes to a text file in D and writes several lines of text to it.
* If there is duplicate content on the secondary disk, delete the duplicate content and write a new content.
*/
Publicclass Testwritetext
{
publicstatic void Main (string[] args)
{
Declares a thrown IO exception in the Main method.
A string that defines the write path.
String fileName = "D:\\hello.txt";
Try
{
Use a FileWriter class that is oriented to character streams.
Calls the construction method directly, passing the write path. The FileWriter class is constructed with five methods.
Filewriterwriter = new FileWriter (fileName);
The write () method of the FileWriter class writes characters to the file.
Writer.write ("hello!\n");
Writer.write ("Thisis My A-file,\n");
Writer.write ("I ' mvery happy!\n");
Writer.write ("Everyday is a new day!\n");
Writer.write ("I love my family.") ");
Clears the contents of the stream and closes it, and the program ends if you do not call the method and do not have all the data written.
Writer.close ();
}
catch (IOException Iox)
{
System.out.println ("problemwriting" + fileName);
}
}
}
If we call this construct method: FileWriter (filename,true), repeat is not allowed, and if this file exists on the D: disk, duplication is not allowed.
Object FileWrite class with character-oriented streams
We can see that Hello.txt is an ordinary ASCII text file, one byte for each English character and two bytes in Chinese characters.
The strings in the Java program are two bytes per character, Unicode encoded.
If you want to write more, you should use an efficient buffer stream class: BufferedWriter.
Unlike FileWriter, multiple newline () methods are used to wrap a line.
We use BufferedWriter to complete the Hello.txt content.
importjava.io.*;
/*
* Writes to a text file in D and writes several lines of text to it.
* Using BufferedWriter is more efficient and can be wrapped.
*/
Publicclass Testbufferedwriter
{
publicstatic void Main (string[] args)
{
Declares a thrown IO exception in the Main method.
A string that defines the write path.
String fileName = "D:\\newhello.txt";
Try
{
Use a BufferedWriter class that is oriented to character streams.
It's equivalent to a layer of pipe on the FileWriter class.
Bufferedwriterout = new BufferedWriter (new FileWriter (FileName));
The write () method of the FileWriter class writes characters to the file.
Out.write ("newhello!\n");
Out.newline ();
Out.write ("thisis bufferwriter paradigm, \ n");
Out.newline ();
Out.write ("keepalone\n");
Out.newline ();
Out.write ("lovelife\n");
Clears the contents of the stream and closes it, and the program ends if you do not call the method and do not have all the data written.
Out.close ();
}
catch (IOException Iox)
{
System.out.println ("problemwriting" + fileName);
}
}
}
Read the text file.
Reader
FileReader
BufferedReader and ReadLine ();
Reads the text from the Hello.txt and displays it on the screen.
importjava.io.*;
/*
* Read the data in the file from the Hello.txt in D disk to the command line.
*
*/
Publicclass Testbufferedreader
{
publicstatic void Main (string[] args)
{
Declares a thrown IO exception in the Main method.
A string that defines the write path.
String fileName = "D:\\hello.txt";
String Line;
Try
{
Using BufferedReader sleeve in FileReader, the processing flow is set on the node stream.
Bufferedreaderin = new BufferedReader (new FileReader (FileName));
Reads the contents of a row first.
line = In.readline ();
Determines whether the read-write content is empty.