Link to home
Start Free TrialLog in
Avatar of chanbrothers
chanbrothers

asked on

Creating and reading a .txt using java

How do i create and read a .txt file using java??
Avatar of eci
eci

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.

Avatar of chanbrothers

ASKER

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();
          }
     }
}
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();
          }
     }
}
ASKER CERTIFIED SOLUTION
Avatar of eci
eci

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.