This has to do with writing and reading binary files [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
This is the steps im supposed to go through. I tried writing the files but i think its wrong.
Creates 10 binary files in a folder called “binaryfiles”. The files must be named “temp0.dat”, “temp1.dat”, etc. to “temp9.dat”.
In each file, write 20 random doubles between 0 and 500. Inclusivity doesn’t matter.
Once the files are written, open each one in sequence from “temp0.dat” to “temp9.dat” and read them one character at a time. As you read the files, print the characters to the output window. Most of the characters will look like Chinese characters.
public class Homework7 {
public static void main(String[] args) throws IOException {
File file = new File("binaryfiles");
file.listFiles();
System.out.println("We have a file" + file);
System.out.println("Does it exist" + file.exists());
System.out.println("?" + file.isDirectory());
Random random = new Random(20);
random.setSeed(500);
double num = random.nextDouble();
OutputStream outStream = new FileOutputStream(file);
outStream.write((int) num);
outStream.close();
}

All files are binary at the lowest level (we talk about different types of files because we choose to interpret the bytes as something else on a higher level) and you create one using an OutputStream and then you write to it either using the stream directly or by using something that writes to the stream for you.
I'm not going to solve this for you since it sounds like a learning assignment, so instead I suggest you look closer at FileOutputStream and DataOutputStream.

Related

What would be a good program that includes writing data to a text or csv file and/or reading from a text or csv file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am still a beginner in java we are task to create a program that includes writing data to a text or csv file and/or reading from a text or csv file.A program that can help or any modern problem. any simple idea?
If you want to perform this task as fast as possible, I suggest you looking at this library that handles CSV file I/O operations:
Apache Commons CSV
Other resources at this link: https://github.com/akullpp/awesome-java#CSV
For a homemade solution try to implement by yourself the logic using Java Core with BufferedReader to read a file line by line, and FileWriter for write strings to append in the file.
BufferedReader example:
BufferedReader csvReader = new BufferedReader(new FileReader(pathInputFile));
while ((row = csvReader.readLine()) != null) {
// do for each row something
// if you use a csv get every values using split method:
// String[] data = row.split(",");
}
csvReader.close();
FileWriter example:
FileWriter fileWriter = new FileWriter(pathOutputFile);
// use csvWriter.append(data) for write strings
// If you want to use a csv structure:
//csvWriter.append("Value1");
//csvWriter.append(",");
//csvWriter.append("Value2");
//if a csv line is finished go down with \n token
//csvWriter.append("\n");
//when finish the write operation
csvWriter.flush();
csvWriter.close();

Java; Txt file of single integer on each line, how can I single out each integer to use in my program? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a txt file, each line has a single integer. I need to make a class that singles out each one, so I can then put them in an array in a different class.
I understand that it may be weird to want each single integer but I need it this way for my program.
Any help would be greatly appreciated.
Have a look at the Scanner class: https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
More specifically, you can use Scanner.nextInt to read into whatever data structure you want.
Example reading from system input (I will leave the exercise to you to figure out how to read from a text file):
Scanner sc = new Scanner(System.in);
List<Integer> numberList = new ArrayList<>();
while (sc.hasNextInt()) {
numberList.add(sc.nextInt());
}
The simple solution would to be to use Scanner class method nextInt() which would read the integers for you.
One of the Scanner constructor takes in argument as the input stream to which you can pass the InputStream corresponding to your file that you want to read.
Below will be the code then to read all the integers:
while(scanner.hasNext()){
System.out.println(scanner.nextInt());
}
Remember the file should only have integers in this case. I will leave upto you to create inputStream for your file!

Reading serialized file - java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I am having problems reading from a serialized file.
More specifically, I have serialized an object to a file written in a hexadecimal format. The problem occurs when I want to read one line at a time from this file. For example, the file can look like this:
aced 0005 7372 0005 5465 7374 41f2 13c1
215c 9734 6b02 0000 7870
However, the code underneath reads the whole file (instead of just the first line). Also, it automatically converts the hexadecimal data into something more readable: ¬ísrTestAòÁ
....
try (BufferedReader file = new BufferedReader(new FileReader(fileName))) {
read(file);
} catch ...
....
public static void read(BufferedReader in) throws IOException{
String line = in.readLine();
System.out.println(line); // PROBLEM: This prints every line
}
}
This code works perfectly fine if I have a normal text file with some random words, it only prints the first line. My guess is the problems lies in the serialization format. I read somewhere (probably the API) that the file is supposed to be in binary (even though my file is in hexadecimal??).
What should I do to be able to read one line at a time from this file?
EDIT: I have gotten quite a few of answers, which I am thankful for. I never wanted to deserialize the object - only be able to read every hexadecimal line (one at a time) so I could analyze the serialized object. I am sorry if the question was unclear.
Now I have realized that the file is actually not written in hexadecimal but in binary. Further, it is not even devided into lines. The problem I am facing now is to read every byte and convert it into hexadecimal. Basically, I want the data to look like the hexadecimal data above.
UPDATE:
immibis comments helped me solve this.
"Use FileInputStream (or a BufferedInputStream wrapping one) and call read() repeatedly - each call returns one byte (from 0 to 255) or -1 if there are no more bytes in the file. This is the simplest, but not the most efficient, way (reading an array is usually faster)"
The file does not contain hexadecimal text and is not separated into lines.
Whatever program you are using to edit the file is "helpfully" converting it into hexadecimal for you, since it would be gibberish if displayed directly.
If you are writing the file using ObjectOutputStream and FileOutputStream, then you need to read it using ObjectInputStream and FileInputStream.
Your question doesn't make any sense. Serialized data is binary. It doesn't contain lines. You can't read lines from it. You should either read bytes, with an InputStream, or objects, with an ObjectInputStream.

how to read a binary file one line at a time in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to read binary data from a file one line at a time. But it seems I can only read n bytes at a time using FileInputStream, any suggestions?
FileInputStream inputStream = new FileInputStream("C:/dir/testfile");
byte[] buffer = new byte[3480];
inputStream.read(buffer);
Binary files don't have lines. Therefore you can't read lines.
There's no such thing as a "line" in a binary file.
It's either a single "thing" or it has a specific, documented format based on records / fields that tells you how many bytes to read for each (or how they're terminated) and what they represent.
(moved from comment)

Will this print a file? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
What would this code do?
FileStream object = new FileStream("c:\input_final.txt",true);
I thought it would not run because they are not two \ and therefore see it as a escape character. Am I wrong and would it print/show/open the file?
No. It will not, because it is not legal code.
Unless you have a custom class FileStream (which you have not posted) then I assume you meant one of -
FileOutputStream which will not work because c:\input_final.txt is not a valid String; \i is not a valid escape sequence.
and
FileInputStream does not have a constructor that takes a boolean as the second argument.
Will this print a file?
No.
It is not valid code. It won't compile ... let alone run!
It is most likely that you mean something like this:
... = new FileOutputStream("c:\\input_final.txt",true);
which says "create an output stream for appending to file "c:\input_final.txt". But even that WILL NOT "print" or "show" the file, or "open" it in an application / browser / editor or whatever.

Categories

Resources