?
Solved

Creating and reading a .txt using java

Posted on 2003-02-24
6
Medium Priority
?
244 Views
Last Modified: 2010-03-31
How do i create and read a .txt file using java??
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
  • 2
  • 2
6 Comments
 

Expert Comment

by:eci
ID: 8014484
import java.io.*;
class WriteReadFile
{
     public static void main(String args[])
          {try {
               FileInputStream reader = new FileInputStream("data");
               FileOutputStream writer = new FileOutputStream("copy");
               int input;
               while((input=reader.read())!=-1)
                    writer.write(input);
               reader.close();
               writer.close();
          } catch (Exception ex) {
               ex.printStackTrace();
          }
          }
}

I think it can help you to do.

0
 

Author Comment

by:chanbrothers
ID: 8014880
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
 

Author Comment

by:chanbrothers
ID: 8014920
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
 

Accepted Solution

by:
eci earned 80 total points
ID: 8015067
Try doing this one.
 String data;
 BufferedReader temp = new BufferedReader(new FileReader("Database.txt"));
 while ( (data = temp.readLine()) != null) {
       System.out.println(data);
 }
0
 

Expert Comment

by:CleanupPing
ID: 9059100
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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses

719 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