?
Solved

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

Posted on 2003-02-25
8
Medium Priority
?
5,260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

764 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