Reading and writing to a file in java
#16
Posted 27 April 2008 - 05:51 PM
Hi can u please explain how to save tokenized items.
that is i've read the item from a file and tokenized but then I want to save it so I can work with the data later on, how do I do that?
that is i've read the item from a file and tokenized but then I want to save it so I can work with the data later on, how do I do that?
#17
Posted 27 April 2008 - 11:56 PM
a tokenized item followed by a newline and append it into the string ......... and save it to the file.... can you show the code based on my algorithm?
#19 Guest_amit*
Posted 08 March 2010 - 10:38 PM
Hi All,
I need to read a text file in java , But this text file is created from a .msg(Outlook message) file ,
I am getting some data in the mail from some user, I have filter the data out from that mail. Is there any
way that i can do it.
My file contain data along with some text.
Thanks and Regards,
Amit Kumar Singh
amiagni@gmail.com
I need to read a text file in java , But this text file is created from a .msg(Outlook message) file ,
I am getting some data in the mail from some user, I have filter the data out from that mail. Is there any
way that i can do it.
My file contain data along with some text.
Thanks and Regards,
Amit Kumar Singh
amiagni@gmail.com
#21
Posted 07 October 2010 - 11:33 PM
It's a nice tutorial but do you know how to add an entry? I don't know how that's why I am asking.
For example I have already a file/records on that specific stuff.txt my professor asked us to add a new entry, to delete that certain entry and to edit a certain entry, but I don't know what to do, we haven't discussed that much about file handling.
For example I have already a file/records on that specific stuff.txt my professor asked us to add a new entry, to delete that certain entry and to edit a certain entry, but I don't know what to do, we haven't discussed that much about file handling.
#23
Posted 25 January 2011 - 08:48 PM
dragon-slayer, on 12 May 2007 - 03:29 PM, said:
This is a very basic way to write to and read from a file in Java using the java.io package.
Firstly we shall cover reading a file
.:READING:.
Easy as that. Remember when handling a BufferedReader to always put it inside a try/catch block that catches an IOException; you'll find this will make it easier as it will output useful error information.
Replace helloworld.txt with the file name you want to read.
This is all good, but what if you want to read a specific line in a File that contains multiple lines? Well it's just as easy.
Let's say helloworld.txt contains this:
and I want to read line 3 which is "hello mouse":
This is how it's done
Easy isn't it? Replace the integer for linenum to the specific line you which to read.
And that's all for Reading from a file.
.:WRITING TO A FILE:.
Writing to a file in Java is just as easy. This is how it's done
Yep that easy. Run this, then open the file you were writing to, and you will see it will say what you wrote out; in my case Hello 83413. Wait, there seems to be a problem. What happened to the rest of the lines? :0 Well, there is an easy fix for that
This is how you can write to a file without erasing its content.
There. Now every time you write to the file it will keep its contents and write to the next line
Thanks for reading my tutorial sorry if it's a bit shabby it's my first time.
Firstly we shall cover reading a file
.:READING:.
import java.io.*;
public class File {
BufferedReader in;
String read;
public File(){
try {
//open a bufferedReader to file helloworld.txt
in = new BufferedReader(new FileReader("helloworld.txt"));
//read a line from helloworld.txt and save into a string
read = in.readLine();
//print out the line
System.out.println("file output: " + read);
//safely close the BufferedReader after use
in.close();
}catch(IOException e){
System.out.println("There was a problem:" + e);
}
}
public static void main(String[] args){
File File = new File();
}
}
Easy as that. Remember when handling a BufferedReader to always put it inside a try/catch block that catches an IOException; you'll find this will make it easier as it will output useful error information.
Replace helloworld.txt with the file name you want to read.
This is all good, but what if you want to read a specific line in a File that contains multiple lines? Well it's just as easy.
Let's say helloworld.txt contains this:
hello world hello keyboard hello mouse hello test
and I want to read line 3 which is "hello mouse":
This is how it's done
import java.io.*;
public class File {
BufferedReader in;
String read;
//the specific line i want to read
int linenum = 3;
public File(){
try {
in = new BufferedReader(new FileReader("helloworld.txt"));
while(linenum > 0){
//read the next line until the specific line is found
read = in.readLine();
linenum--;
}
System.out.println("file output: " + read);
in.close();
}catch(IOException e){
System.out.println("There was a problem:" + e);
}
}
public static void main(String[] args){
File File = new File();
}
}
Easy isn't it? Replace the integer for linenum to the specific line you which to read.
And that's all for Reading from a file.
.:WRITING TO A FILE:.
Writing to a file in Java is just as easy. This is how it's done
import java.io.*;
public class File {
BufferedWriter out;
String read;
int linenum = 3;//the specific line i want to read
public File(){
try {
//replace helloworld.txt with the name of the file
out = new BufferedWriter(new FileWriter("helloworld.txt"));
//Write out the specified string to the file
out.write("Hello 83743");
//flushes and closes the stream
out.close();
}catch(IOException e){
System.out.println("There was a problem:" + e);
}
}
public static void main(String[] args){
File File = new File();
}
}
Yep that easy. Run this, then open the file you were writing to, and you will see it will say what you wrote out; in my case Hello 83413. Wait, there seems to be a problem. What happened to the rest of the lines? :0 Well, there is an easy fix for that
This is how you can write to a file without erasing its content.
import java.io.*;
public class File {
BufferedWriter out;
public File(){
try {
//replace helloworld.txt with the name of the file CHANGE after
//you declare which file to write to add the boolean true which
// will stop it from replacing the helloworld.txt with a new one.
out = new BufferedWriter(new FileWriter("helloworld.txt",true));
//Write out a string to the file
out.write("Hello you stupid duck");
//write a new line to the file so the next time you write
//to the file it does it on the next line
out.newLine();
//flushes and closes the stream
out.close();
}catch(IOException e){
System.out.println("There was a problem:" + e);
}
}
public static void main(String[] args){
File File = new File();
}
}
There. Now every time you write to the file it will keep its contents and write to the next line
Thanks for reading my tutorial sorry if it's a bit shabby it's my first time.
thAnks.....
for your code.....
#24
Posted 08 March 2011 - 12:15 AM
hey, hey...!!
very strange example...!!
today I obtained INT and wanna save it inside
the file,
I put directly using
BufferedWriter out.write(INT VALUE HERE);
it's working but then once I read it,
it read strangely.
Seems I need to cast it into String first.
so with DOUBLE QUOTE it helped me.
Here;
BufferedWriter out.write(""+INT VALUE HERE);
uh oh... great thanks!!
very strange example...!!
today I obtained INT and wanna save it inside
the file,
I put directly using
BufferedWriter out.write(INT VALUE HERE);
it's working but then once I read it,
it read strangely.
Seems I need to cast it into String first.
so with DOUBLE QUOTE it helped me.
Here;
BufferedWriter out.write(""+INT VALUE HERE);
uh oh... great thanks!!
#25
Posted 27 March 2012 - 01:12 AM
castle1200, on 25 January 2011 - 08:48 PM, said:
dragon-slayer, on 12 May 2007 - 03:29 PM, said:
This is a very basic way to write to and read from a file in Java using the java.io package.
Firstly we shall cover reading a file
.:READING:.
Easy as that. Remember when handling a BufferedReader to always put it inside a try/catch block that catches an IOException; you'll find this will make it easier as it will output useful error information.
Replace helloworld.txt with the file name you want to read.
This is all good, but what if you want to read a specific line in a File that contains multiple lines? Well it's just as easy.
Let's say helloworld.txt contains this:
and I want to read line 3 which is "hello mouse":
This is how it's done
Easy isn't it? Replace the integer for linenum to the specific line you which to read.
And that's all for Reading from a file.
.:WRITING TO A FILE:.
Writing to a file in Java is just as easy. This is how it's done
Yep that easy. Run this, then open the file you were writing to, and you will see it will say what you wrote out; in my case Hello 83413. Wait, there seems to be a problem. What happened to the rest of the lines? :0 Well, there is an easy fix for that
This is how you can write to a file without erasing its content.
There. Now every time you write to the file it will keep its contents and write to the next line
Thanks for reading my tutorial sorry if it's a bit shabby it's my first time.
Firstly we shall cover reading a file
.:READING:.
import java.io.*;
public class File {
BufferedReader in;
String read;
public File(){
try {
//open a bufferedReader to file helloworld.txt
in = new BufferedReader(new FileReader("helloworld.txt"));
//read a line from helloworld.txt and save into a string
read = in.readLine();
//print out the line
System.out.println("file output: " + read);
//safely close the BufferedReader after use
in.close();
}catch(IOException e){
System.out.println("There was a problem:" + e);
}
}
public static void main(String[] args){
File File = new File();
}
}
Easy as that. Remember when handling a BufferedReader to always put it inside a try/catch block that catches an IOException; you'll find this will make it easier as it will output useful error information.
Replace helloworld.txt with the file name you want to read.
This is all good, but what if you want to read a specific line in a File that contains multiple lines? Well it's just as easy.
Let's say helloworld.txt contains this:
hello world hello keyboard hello mouse hello test
and I want to read line 3 which is "hello mouse":
This is how it's done
import java.io.*;
public class File {
BufferedReader in;
String read;
//the specific line i want to read
int linenum = 3;
public File(){
try {
in = new BufferedReader(new FileReader("helloworld.txt"));
while(linenum > 0){
//read the next line until the specific line is found
read = in.readLine();
linenum--;
}
System.out.println("file output: " + read);
in.close();
}catch(IOException e){
System.out.println("There was a problem:" + e);
}
}
public static void main(String[] args){
File File = new File();
}
}
Easy isn't it? Replace the integer for linenum to the specific line you which to read.
And that's all for Reading from a file.
.:WRITING TO A FILE:.
Writing to a file in Java is just as easy. This is how it's done
import java.io.*;
public class File {
BufferedWriter out;
String read;
int linenum = 3;//the specific line i want to read
public File(){
try {
//replace helloworld.txt with the name of the file
out = new BufferedWriter(new FileWriter("helloworld.txt"));
//Write out the specified string to the file
out.write("Hello 83743");
//flushes and closes the stream
out.close();
}catch(IOException e){
System.out.println("There was a problem:" + e);
}
}
public static void main(String[] args){
File File = new File();
}
}
Yep that easy. Run this, then open the file you were writing to, and you will see it will say what you wrote out; in my case Hello 83413. Wait, there seems to be a problem. What happened to the rest of the lines? :0 Well, there is an easy fix for that
This is how you can write to a file without erasing its content.
import java.io.*;
public class File {
BufferedWriter out;
public File(){
try {
//replace helloworld.txt with the name of the file CHANGE after
//you declare which file to write to add the boolean true which
// will stop it from replacing the helloworld.txt with a new one.
out = new BufferedWriter(new FileWriter("helloworld.txt",true));
//Write out a string to the file
out.write("Hello you stupid duck");
//write a new line to the file so the next time you write
//to the file it does it on the next line
out.newLine();
//flushes and closes the stream
out.close();
}catch(IOException e){
System.out.println("There was a problem:" + e);
}
}
public static void main(String[] args){
File File = new File();
}
}
There. Now every time you write to the file it will keep its contents and write to the next line
Thanks for reading my tutorial sorry if it's a bit shabby it's my first time.
thAnks.....
for your code.....
thnks..but how can i trace hello are repeated 3 times....
#26
Posted 19 April 2012 - 02:07 PM
thnks it helps me alot..but how can i trace 'hello' are repeated 3 times....
#27
Posted 04 September 2012 - 01:53 PM
My IT teacher prefers to use a scanner to read a text file and then a loop with another scanner to read each line of the text file until the end of the file. This proves to have better data validation, error checking but is perhaps a bit longer in code and the Scanner class in Java is notorious from some strange errors. It also allows for delimeters and as far as I know it is easier in that respect to a read buffer.
Scanner scFile = new Scanner (new File("helloworld.txt")); //create the first scanner and use I/O class to open the file
while(scFile.hasNext())//run the loop to the end of the file
{
//use a scanner for a new line and also can have a delimeter string
Scanner scLine = new Scanner (scFile.nextLine()).useDelimeter("#");
//From here you can use local variables or fields with the scLine.next() or .nextInt() ect... to save each line
//or variable from the file
//close scLine stream
scLine.close();
}
//close the Scanner stream
scFile.close;
|
|







MultiQuote



|