Reading Data From .dat File in Java
-
December 28th, 2010,ten:24 AM #1
Assistance: Reading a *.DAT file in Java
hullo everyone, I promise someone can assistance me.
I have to read some information from a *.dat file and the problem is that it doesn't read it.
I don't know weather I have used the correct code or the location of the file is wrong.The location of the file is in the projection main folder and i call back it reads the file as if ,instead of using waypoints.dat i use waypoint.dat (withouth the s) it catches an exception maxim no file was read.
here is the code
Code:
import coffee.io.*; import java.util.*; public class readWaypoint { /** * @param args */ public static void principal(String[] args) { // TODO Automobile-generated method stub String waypoints=""; Boolean endOfFile=false; try { FileInputStream file = new FileInputStream("waypoints.dat"); DataInputStream fileStream= new DataInputStream(file); //waypoints=fileStream.readUTF(); while(endOfFile==false) { endeavour { waypoints=waypoints + fileStream.readUTF(); } catch(EOFException e) { endOfFile=true; } fileStream.close(); } Organization.out.println(" This is the content of the file:"); System.out.println(waypoints); } catch(FileNotFoundException due east) { Organization.out.println("No file was read"); } take hold of(IOException e) { System.out.println("There was a problem reading the file" + east); } } }
waypoints=waypoints + fileStream.readUTF();thanks in accelerate for the assistance
Last edited by cpu2007; December 28th, 2010 at 01:fourteen PM.
-
Dec 28th, 2010,12:53 PM #2
Re: Assistance: Reading a *.DAT file in Java
Was the .dat file written using the DataOutputStream.writeUTF() method?
-
December 28th, 2010,01:thirteen PM #3
Re: Aid: Reading a *.DAT file in Java
Originally Posted by keang
I remember the trouble was with the syntax and using the wrong reading system.
I have managed to make it work
hither is the code if someone else might need some helpeCode:
import java.io.*; public class readFile { public Cord readIn(String fileIn){ String fileContent=""; try { //make filereader object to read the file FileReader file = new FileReader(fileIn); //create bufferreader to wrap the file BufferedReader fileStream = new BufferedReader(file); Cord temp=fileStream.readLine(); while(temp!=null) { fileContent=fileContent + " " +temp; temp=fileStream.readLine(); } fileStream.close(); } catch(FileNotFoundException e) { System.out.println("No file was read"); } catch(IOException e) { Organization.out.println("There was a problem reading the file"); } return fileContent; } }
Cheers
-
Dec 28th, 2010,01:30 PM #4
Re: Assist: Reading a *.DAT file in Java
I call up the problem was with the syntax and using the wrong reading system.
This is a class that does go equally input the name of the file and then reads the content of it.
- If an IOException is thrown whilst reading the file, the file will not be closed. Y'all demand to close the file in a finally clause.
- Concatenating strings in loops become increasingly inefficient as the length of the file increases. You should use a StringBuilder.
- The terminal result isn't a true representation of the file as a ' ' char is added at the stop of each line rather than a line terminator.
-
Dec 28th, 2010,01:54 PM #5
Re: Help: Reading a *.DAT file in Java
Originally Posted by keang
-what exercise y'all advise to close the file? to put information technology at the terminate of the code or how would i implement a finally clause to my lawmaking ?
-Thanks for that, I will read how the string architect works and if it doesn't require much modification to the lawmaking I might implement that
-how would a line terminator piece of work ? like this fileContent=fileContent +temp + /n;
is that how you put a new line terminator?
-
December 28th, 2010,02:17 PM #6
Re: Help: Reading a *.DAT file in Java
I take written a few guides on this:
Using the finally clause to close a stream
Why you should employ StringBuilderEqually for the line terminator, it depends on what Bone you are on. Luckily there is a system property which supplies the appropriate ane.
-
December 28th, 2010,02:xix PM #7
Re: Help: Reading a *.DAT file in Java
Originally Posted by keang
hewettswitted1985.blogspot.com
Source: https://forums.codeguru.com/showthread.php?506923-Help-Reading-a-%2A-DAT-file-in-Java
0 Response to "Reading Data From .dat File in Java"
Postar um comentário