Reading lines from a text file using The BufferedReader class

B

Bob

i have to make a program that creates a file called input.txt.
the user goes into this file and types a list of words in a line on
many lines
the program is suppose to make a file called output.txt that takes
every word from input.txt and displays one per line. words that are
made out of four letters get replaced to four stars, ****.

i did the program, the only problem is, it will only read the first
line of the input.txt file for some reason. Here is my code from the
reading of input.txt and writing of output.txt if u can tell me why it
won't read the second line in input.txt that owuld be great!

public static void readFile ()
{

String word = "", line = "";
BufferedReader input;
PrintWriter output;

try
{
input = new BufferedReader (new FileReader ("Input.txt"));
output = new PrintWriter (new FileWriter ("Output.txt"));

line = input.readLine ();

while (line != null)
{
try
{
for (int i = 0 ; i < line.length () ; i++)
{
if (line.charAt (i) == ' ')
{
if (word.length () == 4)
output.println ("****");
else
{
output.println (word);
}
word = "";
}
else
word += line.charAt (i);
}
if (word.length () == 4)
output.println ("****");
else
output.println (word);

line = input.readLine ();

output.close ();

} //end try
catch (NullPointerException e2)
{
}
} //end while != null
} //end try
catch (IOException e)
{
}
}


thnx a lot guys ;)
 
N

NOBODY

-stop silently catching exceptions...
-read the jdk doc



public static void readFile() {
BufferedReader input = null;
PrintWriter output = null;

try {
input = new BufferedReader(new FileReader("Input.txt"));
output = new PrintWriter(new BufferedWriter(new
FileWriter("Output.txt")));

String line = null;
while( (line=input.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, "\r\n
\t\f ");
while(st.hasMoreTokens()) {
String word = st.nextToken();
if(word.length()==4)
output.println("****");
else
output.println(word);
}
}

output.flush();

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
input.close();
} catch (IOException e1) {
e1.printStackTrace();
}
output.close();
}
}
 
A

Alex Kizub

Bob said:
i did the program, the only problem is, it will only read the first
line of the input.txt file for some reason.

It reads all lines. You only close output too soon.
Move this close line outside the while loop. You close it for each line :)

Alex Kizub.
 
Joined
Feb 17, 2009
Messages
1
Reaction score
0
Plz Help Me

Hello Guys i want to send the line by line from a text file
i.e. 1st line will send first and 2nd line will send second time and so on

here is the code for sending random line, and i dont want to send random line
I want to send Line By Line


private void selectAMsg() throws FileNotFoundException, IOException,
MessagingException
{

// getting the message file path from input and making buffered Reader
// object
BufferedReader msgFile = new BufferedReader(
new FileReader("C://tips.txt"));

// creating empty arraylist for message
List msgs = new ArrayList();

String str = "";
// Loop that read every line from the message file and put that in to
// message arraylist
while ((str = msgFile.readLine()) != null)
{
if (str.trim().length() != 0)
{
msgs.add(str);
}
}

// Getting random int in the range of the element of arraylist
int randomInt = (int)(Math.random() * msgs.size());

// Getting random message
String randomMessage = (String)msgs.get(randomInt);

sendMail(randomMessage);

}

Please Reply me Soon I m in trouble
Thank You
 
Joined
Jul 15, 2012
Messages
1
Reaction score
0
Simple Solution to File Read

I know this post was years ago, however I am a new programmer and was trying to accomplish the same thing. I'm sure someone else will find this post just as I have. I wanted to accomplish reading text in from a file, storing it to a String while keeping the exact format which the text file has, all while being simple and easy to remember. Believe it or not I could not find any good examples of this so I came up with my own code.
Here it is:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class ReadFromFile {
static File file1 = new File("C:\\newDir\\test.txt");
static FileReader fileReader;

public static void main(String[] args) throws IOException {

fileReader = new FileReader(file1);

BufferedReader br = new BufferedReader(fileReader);

Scanner scan = new Scanner(br);

while (scan.hasNext()) { // iterate through all tokens in the text file

String str = new String(); // create a new string

str = scan.nextLine();// while scanning text, store each line to a
// new string

try {

System.out.println(str); // print all lines to console

} catch (Exception e) {

e.printStackTrace();
}

}

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top