How to Open a .Dat File in Java Program

What is .dat file format in java-io?

It is just a data file, no standards are defined. It can be character data, byte stream.
eg. GOM video player tries to interpret .dat as byte stream.
but u can also create a character stream, rite to file with extension .dat. GOM player just says unable to read.

How to write and read dat file

You should avoid adding -1 to input. The problem is at line:
number.add(numbers = input.nextInt()); in loop.

EDIT:
To write binary data you should use DataInputStream/DataOutputStream. You cannot mix it with Scanner as that is primarily for text data. A sample example is:

public static void main(String[] args) throws IOException {
writeNumbers();
readNumbers();
}

private static void writeNumbers() throws IOException {
DataOutputStream output = new DataOutputStream(new FileOutputStream("C://Numbers.dat"));
for (int i = 0; i < 10; i++) {
output.writeInt(i);
System.out.println(i);
}
output.close();
}

private static void readNumbers() throws IOException{
DataInputStream input = new DataInputStream(new FileInputStream("C://Numbers.dat"));
while (input.available() > 0) {
int x = input.readInt();
System.out.println(x);
}
input.close();
}

How do I read in a line from a .dat file in Java that then needs to be separated?

Note: This answer assumes that each column is a definite length (4-digit course ID, 20-character course name, 1-digit credits, 20-character student name, 20-character major, 1-digit grade)

First, you'll want to get a list of every line in the file.

String[] lines = Files.readAllLines(new File("myfile.dat").toPath()).toArray(new String[0]);

Next, process each line by using the substring() method:

for(String line: lines) {
int courseId = Integer.parseInt(line.substring(0, 4));
String studentName = line.substring(4, 24).trim();
// etc...
}

The trim() function removes trailing whitespace.

Java Reading in Info from .dat Files

If your .dat file is a text file which contains patterns you provided, Scanner seems is a good solution for you.

Try this code:

Scanner sc = new Scanner(new File("test.dat"));
Pattern p = Pattern.compile("([\w\d]*)\s*");
while (sc.findWithinHorizon(p, 0) != null)
{
MatchResult m = sc.match();
System.out.println(m.group(1));
}

Reading a .dat file into an array in Java

If you want to write binary data, use DataInputStream/DataOutputStream. Scanner is for text data and you can't mix it.

WriteInts:

import java.io.*;

public class WriteInts {
public static void main(String[] args) throws IOException {
DataOutputStream output = new DataOutputStream(new FileOutputStream(
"data.dat"));

for (int i = 0; i < 100; i++) {
output.writeInt(i);
System.out.println(i);
}

output.close();
}

}

ReadInts:

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class ReadInts {
public static void main(String[] args) throws IOException {
DataInputStream input = new DataInputStream(new FileInputStream(
"data.dat"));

while (input.available() > 0) {
int x = input.readInt();
System.out.println(x);
}

input.close();
}

}


Related Topics



Leave a reply



Submit