Total members 11893 |It is currently Tue Nov 05, 2024 7:39 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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.




_________________
Recommend my post if you found it helpful.


Author:
Newbie
User avatar Posts: 15
Have thanks: 0 time
Post new topic Reply to topic  [ 1 post ] 

  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     -  
 Search records from text file     -  
 Read Arabic text from file     -  
 display the content of text file     -  



Topic Tags

Java Files and I/O






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com