Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Reading lines from a text file using The BufferedReader class

Reply
Thread Tools

Reading lines from a text file using The BufferedReader class

 
 
Bob
Guest
Posts: n/a
 
      12-03-2004
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
 
Reply With Quote
 
 
 
 
NOBODY
Guest
Posts: n/a
 
      12-03-2004
-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();
}
}
 
Reply With Quote
 
 
 
 
Alex Kizub
Guest
Posts: n/a
 
      12-03-2004
Bob wrote:

> 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.

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      12-03-2004
On Fri, 03 Dec 2004 01:36:36 GMT, NOBODY wrote:

> -stop silently catching exceptions...


More on that..
<http://www.physci.org/codes/javafaq.jsp#stacktrace>

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
 
Reply With Quote
 
abhi19_11 abhi19_11 is offline
Junior Member
Join Date: Feb 2009
Posts: 1
 
      02-17-2009
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
 
Reply With Quote
 
tuldom84 tuldom84 is offline
Junior Member
Join Date: Jul 2012
Posts: 1
 
      07-14-2012
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();
}

}

}

}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading strings using BufferedReader.readLine() is too slow wicli_pucli@yahoo.co.uk Java 12 06-29-2007 12:39 AM
reading bytes with BufferedReader? R Java 8 10-21-2006 10:36 AM
To delete few lines and add few lines at the end of a text file using c program Murali C++ 2 03-09-2006 04:45 PM
Improve performance while reading from a BufferedReader. Nishi Bhonsle Java 1 07-20-2004 02:00 PM
Strange spaces when using BufferedReader rkr Java 5 02-18-2004 10:38 AM



Advertisments