• (2 Pages)
  • +
  • 1
  • 2

Reading and writing to a file in java Rate Topic: ***** 4 Votes

#16 Ziz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-April 08

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?
Was This Post Helpful? 0
  • +
  • -

#17 blacksnake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-July 07

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?
Was This Post Helpful? 0
  • +
  • -

#18 AFTERGLOW  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-October 08

Posted 21 October 2008 - 02:18 AM

thanks for the good work . :^:
Was This Post Helpful? 0
  • +
  • -

#19 Guest_amit*


Reputation:

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
Was This Post Helpful? 0

#20 Guest_ben*


Reputation:

Posted 20 April 2010 - 05:45 AM

Thanx for the tip :)
Was This Post Helpful? 0

#21 gelai8134  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 28-January 10

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.
Was This Post Helpful? 0
  • +
  • -

#22 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Posted 23 January 2011 - 03:15 AM

nice, thanks for the tutorial :)
Was This Post Helpful? 0
  • +
  • -

#23 castle1200  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-January 11

Posted 25 January 2011 - 08:48 PM

View Postdragon-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:.

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.....
Was This Post Helpful? 0
  • +
  • -

#24 gumuruh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 09

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!! :D
Was This Post Helpful? 0
  • +
  • -

#25 ct_hunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-March 12

Posted 27 March 2012 - 01:12 AM

View Postcastle1200, on 25 January 2011 - 08:48 PM, said:

View Postdragon-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:.

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....
Was This Post Helpful? 0
  • +
  • -

#26 ct_hunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-March 12

Posted 19 April 2012 - 02:07 PM

thnks it helps me alot..but how can i trace 'hello' are repeated 3 times....
Was This Post Helpful? 0
  • +
  • -

#27 TRex22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-September 12

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;


Was This Post Helpful? 0
  • +
  • -

#28 michellembeach  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-October 13

Posted 10 December 2013 - 10:08 AM

So...silly question - Do I need to first create and save a .txt file in textpad before I can write to it from Java? or is that what is happening when we write the file? Thanks!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2