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
How to Get Summation of Pair(Key) Values in a Map in Java
Serialize a Double to 2 Decimal Places Using Jackson
How to Print Even and Odd Position Characters of an Array of Strings in Java
How to Force a Method to Throw an Exception in Junit Testing
Java.Lang.Noclassdeffounderror: Org/Springframework/Core/Env/Configurableenvironment
How to Sort Integer Digits in Ascending Order Without Strings or Arrays
In Firebase After Uploading Image How to Get Url
How to Get the Autoincremented Id When I Insert a Record in a Table Via Jdbctemplate
How to Convert Date Which I Got from Firebase Server as Am Getting Error Date
How to Have Each Record of Json on a Separate Line
How to Use Jackson to Deserialise an Array of Objects
How to Increase the Java Heap Size Permanently
Jackson @Jsonproperty Not Working If Property Name Not Equal Field Name
Javax.Validation - Allow Null But Validate If the Value Is Not
Spring Boot - Post Method Not Allowed, But Get Works
How to Get the Ip of the Wifi Hotspot in Android
Check If a Given Time Lies Between Two Times Regardless of Date