Java Development-Implementation of reading and writing txt files

Source: Internet
Author: User

Project Structure:

Running effect:

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

The following is the code:

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

/Text/src/com/b510/txt/MyFile. java
Copy codeThe Code is 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 ("displays append 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 a 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 a file
*
* @ Param newStr
* New Content
* @ Throws IOException
*/
Public static boolean writeTxtFile (String newStr) throws IOException {
// Read the content of the original file before writing
Boolean flag = false;
String filein = newStr + "\ r \ n ";
String temp = "";

FileInputStream FCM = 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
FS = new FileInputStream (file );
Isr = new InputStreamReader (FCM );
Br = new BufferedReader (isr );
StringBuffer buf = new StringBuffer ();

// Save the original content of the file
For (int j = 1; (temp = br. readLine ())! = Null; j ++ ){
Buf = buf. append (temp );
// System. getProperty ("line. separator ")
// The delimiter between the row and the row 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 generates 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 (FS! = Null ){
FCM. 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 (){
// Define an empty string to be returned
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.