Java reads the contents of the TXT file. Can be understood as follows:
1, first get a file handle. File File = new file (); File is a handle to the document. There was a network of calls between the two of them. Then you can start making a phone call.
2, through this line to read the information of party A: new FileInputStream (file) At present this information has been read in memory. Then it needs to be interpreted as something that B can understand.
3, since you have used FileInputStream (). Then the corresponding need to use InputStreamReader () This method to interpret the memory just loaded in the data
4, after the completion of interpretation to output AH. Of course it's going to be converted into data that the IO can recognize. Then you need to call the byte code read Method BufferedReader (). Use the ReadLine () method of BufferedReader () to read each row of data in the TXT file.
/////////////////////////////////////////////////////////////////////
Method One, simple use of scanner and printwriter//////
////////////////////////////////////////////////////////////////////
Package Com.stone.demo;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.PrintWriter;
Import Java.util.Scanner;
public class Readandwritetxtbyscanner {
private static void Readtxtfile (String filepath) {
try {
Scanner in=new Scanner (New File (filepath));
while (In.hasnext ()) {
String Str=in.nextline ();
System.out.println (str);
}
} catch (FileNotFoundException e) {
E.printstacktrace ();
}
}
private static void Writetxtfile (String filepath) {
try {
@SuppressWarnings ("resource")
String relativepath=system.getproperty ("User.dir") + "/src/com/stone/demo/writefile.txt";
PrintWriter out=new PrintWriter (relativepath);
Out.write ("12312");
Out.write ("\ n");
Out.write ("12312");
Out.flush ();
Out.close ();
} catch (Exception e) {
E.printstacktrace ();
}
}
public static void Main (string[] args) {
String filepath=system.getproperty ("User.dir") + "/src/com/stone/demo/writefile.txt";
Writetxtfile (filepath);
System.out.println ("==================");
Readtxtfile (filepath);
}
}
/////////////////////////////////////////////////////
Method two: Using IO stream///////////////////
/////////////////////////////////////////////////////
- Package Com.stone.demo;
- Import Java.io.BufferedReader;
- Import Java.io.BufferedWriter;
- Import Java.io.File;
- Import Java.io.FileInputStream;
- Import java.io.FileNotFoundException;
- Import Java.io.FileWriter;
- Import java.io.IOException;
- Import Java.io.InputStreamReader;
- Import Java.util.Scanner;
- Public class Readandwritetxtbyfile {
- /**
- * Function: Java read the content of TXT file steps:
- * 1: Get the file handle first
- * 2: Get the file handle as input a byte stream, need to read this input stream
- * 3: After reading to the input stream, the generated byte stream needs to be read
- * 4: The output of one row. ReadLine (). Note: It is unusual to consider the situation
- * @param FilePath
- */
- public static void Readtxtfile (String filePath) {
- try {
- String encoding = "GBK";
- File File = new file (FilePath);
- if (file.isfile () && file.exists ()) { //Determine if the file exists
- InputStreamReader Read = new InputStreamReader (new FileInputStream (file), encoding); Considering the encoding format
- BufferedReader BufferedReader = new BufferedReader (read);
- String linetxt = null;
- While ((Linetxt = Bufferedreader.readline ()) = null) {
- System.out.println (Linetxt);
- }
- Read.close ();
- }
- } catch (Exception e) {
- SYSTEM.OUT.PRINTLN ("Error reading file contents");
- E.printstacktrace ();
- }
- }
- private static void Writetxtfile (String filepath) {
- File file=new file (filepath);
- BufferedWriter writer = null;
- try {
- if (File.isfile () &&!file.exists ()) {
- System.out.println ("The specified file cannot be found");
- File.createnewfile (); //Does not exist then create
- }
- else{
- //writer = new BufferedWriter (new FileWriter (file,true));//Add true here to not overwrite the contents of the original TXT file to continue writing
- writer = New BufferedWriter (new FileWriter (file));
- Writer.write ("Hello");
- Writer.write ("\ n");
- Writer.write ("Hello");
- }
- } catch (Exception e) {
- E.printstacktrace ();
- } finally {
- if (writer! = null) {
- try {
- Writer.flush ();
- Writer.close ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
- }
- }
- public static void Main (String argv[]) {
- String Filepath=system.getproperty ("User.dir") +"/src/com/stone/demo/txtfile.txt";
- Writetxtfile (filepath);
- System.out.println ("===================");
- Readtxtfile (filepath);
- }
- }
Note: Excerpt from http://blog.csdn.net/giserstone/article/details/40922189
Java read/write TXT file