Java Write and read Demo

Source: Internet
Author: User

The following code is mainly implemented in Java read files and write files, practice the flow operation.

Important: 1. When reading the file, be sure to add the encoding format, otherwise Chinese garbled

Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import java.util.ArrayList;
Import java.util.List;


public class Readerorwrite {
public static void Main (string[] args) {
Readerorwrite rw =new readerorwrite ();
Rw.mywrite ("E:\\parse\\output.txt", "lalalalalaal--this is the content, \ r \ n");
Rw.myread ("E:\\parse\\output.txt");
}
public void Mywrite (String filepath,string content) {
try {
"E:\\parse\\output.txt"
File Writename = new file (FilePath); Relative path, if not, to create a new output. TXT file
Writename.createnewfile (); Create a new file
BufferedWriter out = new BufferedWriter (new FileWriter (Writename));
Out.write (content); \ r \ n is the wrapped file content
Out.flush (); Press the buffer contents into the file
Out.close (); Finally close the file
} catch (IOException e) {
E.printstacktrace ();
}
}

Public list<string> Myread (String pathname) {
list<string> linelist = new arraylist<string> ();
try{
String pathname = "D:\\twitter\\13_9_6\\dataset\\en\\input.txt"; Absolute or relative path, this is an absolute path, and a relative path is shown when writing to a file
File filename = new file (pathname); The input to read the above path. TXT file
InputStreamReader reader = new InputStreamReader (new FileInputStream (filename), "UTF-8"); Create an input stream object reader
Missing Utf-8 will be garbled in Chinese
BufferedReader br = new BufferedReader (reader); Create an object that turns the contents of a file into a language that the computer can read
String line = "";
line = Br.readline ();
while (line! = null) {
Linelist.add (Br.readline ());
line = Br.readline (); Read one row of data at a time
System.out.println (line); Test what is read
}
}catch (Exception e) {
E.printstacktrace ();
}
return linelist;
}
}

Java Write and read Demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.