• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

Creating and reading a .txt using java

How do i create and read a .txt file using java??
0
chanbrothers
Asked:
chanbrothers
  • 2
  • 2
1 Solution
 
eciCommented:
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
 
chanbrothersAuthor Commented:
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
 
chanbrothersAuthor Commented:
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
 
eciCommented:
Try doing this one.
 String data;
 BufferedReader temp = new BufferedReader(new FileReader("Database.txt"));
 while ( (data = temp.readLine()) != null) {
       System.out.println(data);
 }
0
 
CleanupPingCommented:
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

Get expert help—faster!

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

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now