Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Reading and Writing To text file

Sun Apr 08, 2007 7:05 am

This is the code i made to write and read from text file using java.

java code
/*
* Main.java
*
* Created on November 10, 2006, 10:14 AM
*/


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
*
* @author MSI */
public class Main {

/** Creates a new instance of Main */
public Main()

{
data="mmmmmmmmmmmmm";

String ddff;
int tt=6111;

try {

File_in=new FileInputStream(myFile);
Data_in =new DataInputStream(File_in);
BufferedReader ReadH=new BufferedReader(new FileReader("text.txt")) ;
BufferedWriter myW=new BufferedWriter(new FileWriter("text.txt")) ;

File_out=new FileOutputStream(myFile);
Data_out=new DataOutputStream(File_out);
Data_out.writeInt(tt);
Data_out.flush();

Data_out.close();

System.out.println(ReadH.read());
System.out.println(ReadH.read());
System.out.println(ReadH.read());
System.out.println(ReadH.read());
System.out.println(ReadH.read());
System.out.println((Data_in.read(buff)));
System.out.println(buff);
System.out.println(Byte.toString(Data_in.readByte()));

} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}

}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new Main();
}
File myFile=new File("text.txt");
FileInputStream File_in;
DataInputStream Data_in ;
FileOutputStream File_out ;
DataOutputStream Data_out;
String data;
byte[] buff=new byte[100];
byte dd;
int x;

}


To Read from a file you open a logical connection to the file in this part :java code
File_in=new FileInputStream(myFile);
Data_in =new DataInputStream(File_in);

or :
java code
BufferedReader  ReadH=new BufferedReader(new FileReader("text.txt")) ;


To Write to a file you open a logical connection to the file in this part :java code
File_out=new FileOutputStream(myFile);
Data_out=new DataOutputStream(File_out);

or :
java code
BufferedWriter myW=new BufferedWriter(new FileWriter("text.txt")) ;


To read from the file in a buffer :
java code
System.out.println((Data_in.read(buff)));
System.out.println(buff);

To read one charactar from the file :
java code
ReadH.read()

this function return -1 if the end of the file achivied


To write to the file

java code
Data_out.writeInt(tt);

this write integers only. There are more functions to other type of data.
or :

java code
WriteH.write(tt);


using close() and flush() functions
flush() free the buffer of the Read/Write.
close() close the logical connection.




Post a reply
  Related Posts  to : Reading and Writing To text file
 Read XML file content using SAX and writing its as SQL     -  
 Reading the all file in php     -  
 Multithreaded File Reading     -  
 Reading file with integers     -  
 Reading selected data from a source file     -  
 Import text file with ASP.NET     -  
 Create a text file     -  
 Read Arabic text from file     -  
 display the content of text file     -  
 Search records from text file     -  

Topic Tags

Java Files and I/O