Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Text to .dat problem.

807600Oct 16 2007 — edited Oct 16 2007
I'm working on a program that reads a .txt file, line by line, then stores the information in a binary file.
I have gotten the program to create the .dat file, but i inserted some print statements into the reading of the text file and am getting some null values. When i move the data around it will read some of the lines, but not others, and it seems to be totaly random.
The code is fairly long, and i'm not even sure if it's the problem, since it seems to do what i want it to do, aside from not reading the file.
If the text file is the problem, i'd be happy to post it as well, and heres the code.
Please excuse the long filenames for the strings, i was trying to keep track of which line i was on by counting letters. the print statements are going to get commented out, i just had them in to see what the program was reading.
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


public class J9E8
{
public static void main(String args[])
  throws FileNotFoundException, IOException
{
 
File thefile = new File ("studentNames.txt");

 FileReader frdr= new FileReader (thefile);
        
 BufferedReader aBfrd= new BufferedReader(frdr);

 String aRcrd= aBfrd.readLine();
  System.out.println("The file says: " + aRcrd);
          aRcrd= aBfrd.readLine();

String aRRcrd= aBfrd.readLine();
System.out.println("The file says: " + aRRcrd);
aRRcrd=aBfrd.readLine();

String aRRRcrd= aBfrd.readLine();
System.out.println("The file says: " + aRRRcrd);
aRRRcrd=aBfrd.readLine();

String aRRRRcrd= aBfrd.readLine();
System.out.println("The file says: " + aRRRRcrd);
aRRRRcrd=aBfrd.readLine();

String aRRRRRcrd= aBfrd.readLine();
System.out.println("The file says: " + aRRRRRcrd);
aRRRRRcrd=aBfrd.readLine();




   File afile = new File ("studentNames.dat");

  FileOutputStream aFileOutStream= new FileOutputStream (afile);

 DataOutputStream aDataOutStream = new DataOutputStream (aFileOutStream);
 try{
aDataOutStream.writeUTF( aRcrd);
aDataOutStream.writeUTF( aRRcrd);
aDataOutStream.writeUTF( aRRRcrd);
aDataOutStream.writeUTF( aRRRRcrd);
aDataOutStream.writeUTF( aRRRRRcrd);
aFileOutStream.close( );
}

       catch (NullPointerException e)
{
}

File somefile = new File ("studentScores.txt");

 FileReader frrdr= new FileReader (somefile);
        
 BufferedReader aBBfrd= new BufferedReader(frrdr);

 String aCcrd= aBBfrd.readLine();
  System.out.println("The file says: " + aCcrd);
          aCcrd= aBBfrd.readLine();

String aCCcrd= aBBfrd.readLine();
System.out.println("The file says: " + aCCcrd);
aCCcrd=aBBfrd.readLine();

String aCCCcrd= aBfrd.readLine();
System.out.println("The file says: " + aCCCcrd);
aCCCcrd=aBBfrd.readLine();

String aCCCCcrd= aBBfrd.readLine();
System.out.println("The file says: " + aCCCCcrd);
aCCCCcrd=aBBfrd.readLine();

String aCCCCCcrd= aBBfrd.readLine();
System.out.println("The file says: " + aCCCCCcrd);
aCCCCCcrd=aBBfrd.readLine();

String aCCCCCCcrd= aBBfrd.readLine();
System.out.println("The file says: " + aCCCCCCcrd);
aCCCCCCcrd=aBBfrd.readLine();

String aCCCCCCCcrd= aBBfrd.readLine();
System.out.println("The file says: " + aCCCCCCCcrd);
aCCCCCCCcrd=aBBfrd.readLine();

String aCCCCCCCCcrd= aBBfrd.readLine();
System.out.println("The file says: " + aCCCCCCCCcrd);
aCCCCCCCCcrd=aBBfrd.readLine();

String aCCCCCCCCCcrd= aBBfrd.readLine();
System.out.println("The file says: " + aCCCCCCCCCcrd);
aCCCCCCCCCcrd=aBBfrd.readLine();

String aCCCCCCCCCCcrd= aBBfrd.readLine();
System.out.println("The file says: " + aCCCCCCCCCCcrd);
aCCCCCCCCCCcrd=aBBfrd.readLine();

File tfile = new File ("studentScores.dat");

FileOutputStream theFileOutStream= new FileOutputStream (tfile);

 DataOutputStream theDataOutStream = new DataOutputStream (theFileOutStream);


 try{
aDataOutStream.writeUTF( aCcrd);
aDataOutStream.writeUTF( aCCcrd);
aDataOutStream.writeUTF( aCCCcrd);
aDataOutStream.writeUTF( aCCCCcrd);
aDataOutStream.writeUTF( aCCCCCcrd);
aDataOutStream.writeUTF( aCCCCCCcrd);
aDataOutStream.writeUTF( aCCCCCCCcrd);
aDataOutStream.writeUTF( aCCCCCCCCcrd);
aDataOutStream.writeUTF( aCCCCCCCCCcrd);
aDataOutStream.writeUTF( aCCCCCCCCCCcrd);
aFileOutStream.close( );
}

       catch (NullPointerException e)
{
}

}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 13 2007
Added on Oct 16 2007
13 comments
209 views