?
Solved

Converting a .txt file into a String in Java...

Posted on 2003-02-25
8
Medium Priority
?
5,275 Views
Last Modified: 2011-10-03
How do i convert the words in a .txt file into a string in java?? Below is how I created my .txt file.




import java.io.*;

public class WriteReadFileTwo
{
    public static void main(String[] args)
    {
         try
         {
              byte [] buffer = new byte[256];
              FileOutputStream writer = new FileOutputStream("Database.txt");
              String str = "hello! This is my email address!!!";
              buffer = str.getBytes();
              writer.write(buffer);
              writer.close();
             
              FileInputStream reader = new FileInputStream("Database.txt");
             
         }
         catch (Exception e)
         {
              e.printStackTrace();
         }
    }
}
0
Comment
Question by:chanbrothers
6 Comments
 
LVL 1

Accepted Solution

by:
umangjoshi earned 80 total points
ID: 8014967
pls try this:

 FileInputStream fis = new FileInputStream("Database.txt");
 StringBuffer sb = new StringBuffer();

 Reader reader = new InputStreamReader(new java.io.BufferedInputStream(fis));
               
 //read the stream
 int c = 0;
 while ( (c = reader.read()) != -1 )
      sb.append((char) c);
                             
 String output = sb.toString();
0
 
LVL 1

Expert Comment

by:gkasinath
ID: 8014991
Use the above code with a lil modification..

BufferedReader reader = new BufferedReader(
                        new InputStreamReader(
                        new FileInputStream( "Database.txt" )));

while ( (c = reader.readLine()) != null )
     sb.append((char) c);

A NULL is encountered when there is no more line to be read.

Regds
Lupo
                             
0
 
LVL 2

Expert Comment

by:DidierD
ID: 8015354
You can also do something like this

StringBuffer stringBuffer = new StringBuffer();
try{
      FileReader reader = new FileReader("Database.txt");
               
      char[] buffer = new char[1024];
      int c = reader.read(buffer);
     
      while (c !=  -1){
        stringBuffer.append(buffer, 0, c);
        c = reader.read(buffer);
      }
} catch (Exception e){
  e.printStackTrace();
}
String outPut = stringBuffer.toString();
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:TimYates
ID: 8016129
And that looks neater when you do:

StringBuffer stringBuffer = new StringBuffer();
try
{
  FileReader reader = new FileReader("Database.txt");
               
  char[] buffer = new char[1024];
  int c = 0 ;

  while( ( c = reader.read( buffer ) ) > -1 )
  {
    stringBuffer.append(buffer, 0, c);
  }
}
catch( Exception e )
{
   e.printStackTrace();
}
String outPut = stringBuffer.toString();

(as you only do the read in one place rather than two :-) )
0
 

Expert Comment

by:CleanupPing
ID: 9059095
chanbrothers:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9360864
Points should prpbably go to umangjoshi
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month8 days, 11 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question