Java development of Read and write TXT file operation implementation _java

Source: Internet
Author: User

Project structure:

Operation Effect:

========================================================

Here is the Code section:

========================================================

/text/src/com/b510/txt/myfile.java

Copy Code code as follows:

Package com.b510.txt;

Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import Java.io.FileReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.PrintWriter;

/**
* @author Hongten
*
* @time 2011-12-12 2011
*/
public class MyFile {
@SuppressWarnings ("Static-access")
public static void Main (string[] args) {
MyFile MyFile = new MyFile ();
try {
for (int i = 0; i < 5; i++) {
Myfile.creattxtfile ("test");
Myfile.writetxtfile ("Show is appended information" + i);
String str = myfile.readdate ();
System.out.println ("*********\n" + str);
}
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

private static String Path = "txt/";
private static String filenametemp;

/**
* Create File
*
* @throws IOException
*/
public static Boolean creattxtfile (String name) throws IOException {
Boolean flag = false;
filenametemp = path + name + ". txt";
File filename = new file (filenametemp);
if (!filename.exists ()) {
Filename.createnewfile ();
Flag = true;
}
return flag;
}

/**
* Write file
*
* @param newstr
* New Content
* @throws IOException
*/
public static Boolean writetxtfile (String newstr) throws IOException {
Read the original file contents before writing
Boolean flag = false;
String Filein = newstr + "\ r \ n";
String temp = "";

FileInputStream FIS = null;
InputStreamReader ISR = null;
BufferedReader br = null;

FileOutputStream fos = null;
PrintWriter pw = null;
try {
File path
File File = new file (filenametemp);
Read the file into the input stream
FIS = new FileInputStream (file);
ISR = new InputStreamReader (FIS);
br = new BufferedReader (ISR);
StringBuffer buf = new StringBuffer ();

Save the original contents of the file
for (int j = 1; (temp = Br.readline ())!= null; J + +) {
BUF = Buf.append (temp);
System.getproperty ("Line.separator")
The delimiter between rows and rows is equivalent to "\ n"
BUF = Buf.append (System.getproperty ("Line.separator"));
}
Buf.append (Filein);

FOS = new FileOutputStream (file);
PW = new PrintWriter (FOS);
Pw.write (Buf.tostring (). ToCharArray ());
Pw.flush ();
Flag = true;
catch (IOException E1) {
TODO automatically generate catch blocks
Throw E1;
finally {
if (PW!= null) {
Pw.close ();
}
if (FOS!= null) {
Fos.close ();
}
if (br!= null) {
Br.close ();
}
if (ISR!= null) {
Isr.close ();
}
if (FIS!= null) {
Fis.close ();
}
}
return flag;
}

/**
* Read Data
*/
public void ReadData1 () {
try {
FileReader read = new FileReader (filenametemp);
BufferedReader br = new BufferedReader (read);
String Row;
while (row = Br.readline ())!= null) {
System.out.println (row);
}
catch (FileNotFoundException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}

Public String readdate () {
Defines an empty string to return
String STRs = "";
try {
FileReader read = new FileReader (new File (filenametemp));
StringBuffer sb = new StringBuffer ();
Char ch[] = new char[1024];
int d = read.read (CH);
while (d!=-1) {
String str = new String (CH, 0, D);
Sb.append (str);
d = read.read (CH);
}
System.out.print (Sb.tostring ());
String a = sb.tostring (). ReplaceAll ("", ",");
STRs = a.substring (0, A.length ()-1);
catch (FileNotFoundException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
return STRs;
}
}

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.