The first time to write a blog, a simple example: Java read and write TXT file, I hope this is a good start.

Source: Internet
Author: User
Tags readfile

As a junior developer, every day on the network to find someone else's code, and then modify the change to apply to the project, unknowingly, I really become the code of the porter.

The fun of programming is to find the code that you want in the vast ocean of knowledge, to share information, to facilitate yourself, and to facilitate others.

The following code is a simple Java code that reads an instance of a TXT file. Although simple, it can be an introductory example for beginners of the zero base.

Package javatest;

Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStreamReader;

public class Txt {

/*
* Read TXT file
* @param filename
* @return txt content
*/
public static String ReadFile (String fileName) {
String filecontent = ""; Save the Read txt result
try {
File F = new file (fileName);
if (F.isfile () && f.exists ()) {
InputStreamReader read = new InputStreamReader (
New FileInputStream (f), " UTF-8 ");
BufferedReader reader = new BufferedReader (read);
String Line;
while (line = Reader.readline ())! = null) {
Filecontent + = line + "\ n";
}
Read.close ();
}
} catch (Exception e) {
E.printstacktrace ();
}
return filecontent;
}

/*
* Write content to TXT file
* @param filename to be written by filename
* @param contents of content written
*/
public static void WriteFile (String fileName, String Content) {
try {
File F = new file (fileName);
if (!f.exists ()) {
F.createnewfile ();
}
FileOutputStream writer = new FileOutputStream (f);
Writer.write (Content.getbytes ("UTF-8"));
Writer.close ();
} catch (Exception e) {
E.printstacktrace ();
}
}

public static void Main (string[] args) {
String content = ReadFile ("K:/apache1.txt");
String data= "Chunhuaqiuyue when?" The past knows how much. The small building last night and East Wind, the Motherland is the Moon Ming. Shing should be in the same, just days outworn change. How much sorrow can you ask? It is as if a river flows eastward. ";
WriteFile ("k:\\ poppies. txt", data);
SYSTEM.OUT.PRINTLN (content);
}
}

The first time to write a blog, a simple example: Java read and write TXT file, I hope this is a good start.

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.