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.

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

Replies To: Reading and writing to a file in java

not a bad tutorial, but i think the finding of a line should be done in another way, than simply:int linenum = 3;//the specfic line i want to read
since, if the file does not have 3 lines, this will throw an exception. I realize it will be caught, but it is something which could be handled ahead of time.
(Just something to think about)

I also wanted to comment, that the classname File is not the best choice as this class already exists in java.

I am a fan of the bufferedReader myself, over the other options. I think i even used it in some of my snippets

It's good to see an interest in sharing knowledge. I know from experience, that files can be a tricky subject for new/inexperienced java programmers, and an example and explanation is exactly what they need to learn properly.
Keep it up!

hey im new to file reading and writing issues, i would appreciate any help on how to read an int, double,char.. from a file

i want to write to a file an int id char type int miles int time
then i want to read the miles and time to use tem for calculatingf the efficiency which is miles/time

thanks for help

Hello,If i where you I would write the numbers out as a string.
Then read it as a string but just convert it from string to int.This can be done by using the Integer.parseInt(); function.For example you have just written 20 on the first line which repesents the miles.
so when you read it you do int miles = Integer.parseInt(in.readLine());
I hope this helps

I would suggest putting close method into a finally close, so if an IOException is raised, the file is still closed. (This way it isn't, although when the object is finalized, it probably happens)
Also, choosing a name for your class (File) that is the same as an often used core Java class, is probably not a good idea. Anyway, just my 2 cents.