4 Replies - 579 Views - Last Post: 20 August 2012 - 03:47 PM Rate Topic: -----

#1 troyb408  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 18-August 12

Reading txt files in java

Posted 19 August 2012 - 03:46 PM

I've written this class to write text to a file. Now how do I read it? I was assigned to create a class to write text to a file but am confused on reading the file? Supposed to read the information from the text file and display it in a form on record at a time?
import javax.swing.JOptionPane;
import java.io.*;


 //This class will accept perameters and write the information into a file 
 
 
public class WriteToFile 
{    
        String Name, Address,CoverCheckBox,HeaterCheckBox,InGroundCheckBox,AboveGroundCheckBoxPool, 
 PoolCheckBox, SpaCheckBox, SpaCoverCheckBox, HotTubCheckBox, HotTubCoverCheckBox,
 HotTubincludeCheckBox, HotTubAGroundCheckBox; 
 
        public WriteToFile()
        {
            
        }
 public WriteToFile(String name, String address, boolean coverCheckBox, boolean  heaterCheckBox, 
 boolean inGroundCheckBox, boolean aboveGroundCheckBoxPool, boolean poolCheckBox, boolean spaCheckBox,
 boolean spaCoverCheckBox, boolean hotTubCheckBox, boolean hotTubCoverCheckBox, boolean hotTubincludeCheckBox,
 boolean hotTubAGroundCheckBox ) throws IOException
 { 
  
  String str;
  Name = name;
    Address = address;
  
  //The boolString method returns the boolean as a string value
  CoverCheckBox = boolString(coverCheckBox);
  HeaterCheckBox = boolString(heaterCheckBox);
  InGroundCheckBox = boolString(inGroundCheckBox);
  AboveGroundCheckBoxPool = boolString(aboveGroundCheckBoxPool);
  PoolCheckBox = boolString(poolCheckBox);
  SpaCheckBox = boolString(spaCheckBox);
  SpaCoverCheckBox = boolString(spaCoverCheckBox);
  HotTubCheckBox = boolString(hotTubCheckBox);
  HotTubCoverCheckBox = boolString(hotTubCoverCheckBox);
  HotTubincludeCheckBox = boolString(hotTubincludeCheckBox);
  HotTubAGroundCheckBox = boolString(hotTubAGroundCheckBox);
  str = Name + "," + Address + "," + CoverCheckBox + "," + HeaterCheckBox + "," 
                + "," + InGroundCheckBox + "," + AboveGroundCheckBoxPool + "," + PoolCheckBox
  + "," + SpaCheckBox + "," + SpaCoverCheckBox + "," + HotTubCheckBox + "," + HotTubCoverCheckBox
  + "," + HotTubincludeCheckBox + "," + HotTubAGroundCheckBox;
    
  try
  { 
   
   FileWriter writer = new FileWriter("customerInfo.dat", true);   
   
   writer.write(str);    
   
   writer.close();
   
  }
  catch (FileNotFoundException e) 
  {
   JOptionPane.showMessageDialog(null, "Sorry an unexpected"
                                + "error occured");
  }
 
 }//End contructor
 
 public static String boolString(boolean x)
 {
  String str;
  if (x == true)
  {
   str = "true";
  }
  else
  {
   str = "false";
  
  }
  return str;

 }//End boolString method
        
        
        
    }//End class

This post has been edited by jon.kiparsky: 19 August 2012 - 04:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading txt files in java

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5596
  • View blog
  • Posts: 9,030
  • Joined: 19-March 11

Re: Reading txt files in java

Posted 19 August 2012 - 04:04 PM

The easiest way to read text from a file is with the Scanner class. Read the documentation and try to implement something, then ask questions if you run into a problem.

This post has been edited by jon.kiparsky: 19 August 2012 - 04:05 PM

Was This Post Helpful? 1
  • +
  • -

#3 duskog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-August 12

Re: Reading txt files in java

Posted 19 August 2012 - 04:10 PM

Reading content of file:
String contentOfFile = new Scanner(new File("fajl")).useDelimiter("\\Z").next();
Getting data of customer:
String[] customersData = contentOfFile.split(",");
Writing data into file;
PrintWriter out = new PrintWriter(new FileOutputStream(new File("fajl")),true);
out.println("some data");
Was This Post Helpful? 0
  • +
  • -

#4 troyb408  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 18-August 12

Re: Reading txt files in java

Posted 20 August 2012 - 03:31 PM

So if I have the following code. How do I display the lines from the text file in a form:

Is it just a PrintWriter.xxxxx I'm not sure. My overall goal is to read from a text file and the customer name, address, email etc be displayed in a TextArea of a form.

public ReadText()throws IOException
{
String fileName;

//scanner object
String contentOfFile = new Scanner(new File("customerInfo.dat")).useDelimiter("\\Z").next();

String[] customerInfo = contentOfFile.split(",");

PrintWriter out = new PrintWriter(new FileOutputStream(new File("customerInfo.dat")),true);

}
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2146
  • View blog
  • Posts: 8,920
  • Joined: 20-September 08

Re: Reading txt files in java

Posted 20 August 2012 - 03:47 PM

Quote

My overall goal is to read from a text file and the customer name, address, email etc be displayed in a TextArea of a form.


The easiest way to read the whole file into a text area is to do


Reader in = new FileReader("customerInfo.dat");
textArea.read(in, null);
in.close();

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1