You are viewing our Forum Archives. To view or take place in current topics click here.
Java Wizards Assemble
Posted:

Java Wizards AssemblePosted:

CoNdEmR
  • TTG Fanatic
Status: Offline
Joined: Apr 29, 200914Year Member
Posts: 4,420
Reputation Power: 1211
Status: Offline
Joined: Apr 29, 200914Year Member
Posts: 4,420
Reputation Power: 1211
So, i'm having an issue reading some data from a binary file. What the first program does, is take in different types of data, string, int, char etc, and writes them to a binary file. Works fine, no issues(to an extent) there. The problem is with the second program which is meant to read from the file.

It will only read the first iteration of whats actually held in the file and won't read the rest, and even at that it doesn't read all values stored even in the first iteration.

write code:

import java.io.*;
import java.util.*;

public class w7q1_test{

   public static void main(String[] args){

      Scanner in = new Scanner(System.in);
      DataOutputStream dataOut;

      try{

         dataOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("servers.dat")));

         String s;
         int b = 0;
         int c = 0;
         String a="";


            for(int i=0;i<10;i++){
               System.out.println("Enter a name: ");
               a = in.nextLine(); //after first itteration there is a bug that woun't allow me to enter first name - unknown as to why
               dataOut.writeUTF(a);

               System.out.println("Enter a second name: ");
               String n = in.nextLine();
               dataOut.writeUTF(n);

               System.out.println("Enter a 4 digit student number: ");
               b = in.nextInt();
               dataOut.writeInt(b);

               System.out.println("Enter Sex m/f only: ");
               String ch = in.next();
               char v = ch.charAt(0);
               dataOut.writeChar(v);
            }

            dataOut.close();

      }
      catch(Exception e){
         System.err.println(e.toString());
         System.exit(1);

         }

      }


}



read code:

 java.io.*;

public class w7q2{

   public static void main(String[] args){
      DataInputStream datIn = null;
      String dis;
      String ***="";
      int j=0;

      try{

      datIn = new DataInputStream(new BufferedInputStream(new FileInputStream("servers.dat")));
      int i = 0;
            while(true){
               for(int u=0;u<11;u++){

               dis = datIn.readUTF();
               datIn.readUTF();
               j =datIn.readInt();
               datIn.readInt();
               char c=datIn.readChar();
               datIn.readChar();
               System.out.println("Name: "+dis);
               System.out.println("Second name: "+***);
               System.out.println("Student Number: "+j);
               System.out.println("Sex: "+c);

               }

         }



      }

      catch(EOFException e){
            }

               catch(Exception e){
                  System.err.println(e.toString());
                  System.exit(1);
               }


   }
}
#2. Posted:
RDCA
  • TTG Contender
Status: Offline
Joined: Jul 12, 201013Year Member
Posts: 3,612
Reputation Power: 173
Status: Offline
Joined: Jul 12, 201013Year Member
Posts: 3,612
Reputation Power: 173
For the write code you need to add this:


System.out.println("Enter Sex m/f only: ");
String ch = in.next();
in.nextLine();
char v = ch.charAt(0);
dataOut.writeChar(v);


I am still working on the reading.

Alright, I think I need to file for me to be able to figure this out, can you give a sample file?
#3. Posted:
CoNdEmR
  • Shoutbox Hero
Status: Offline
Joined: Apr 29, 200914Year Member
Posts: 4,420
Reputation Power: 1211
Status: Offline
Joined: Apr 29, 200914Year Member
Posts: 4,420
Reputation Power: 1211
You can try reading from this

[ Register or Signin to view external links. ]
#4. Posted:
RDCA
  • TTG Contender
Status: Offline
Joined: Jul 12, 201013Year Member
Posts: 3,612
Reputation Power: 173
Status: Offline
Joined: Jul 12, 201013Year Member
Posts: 3,612
Reputation Power: 173
Alright guys, all we need now is the char of the sex.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.