im tasked with written two java programs. One program creates a file called 'userinput.txt', then writes everything the user inputs into the file. Once done a new file is created called 'Checksum.txt' and this file will write down the checksum for the 'userinput.txt' file after reading whats inside of it.
The 2nd program just reads the same 'userinput.txt' file and then generates a checksum and prints it onto the console (i also have to get the program to read the other checksum.txt file and display it int the console to compare the two but i havent gotten around to that yet).
Iv written the program for these two but my problem is they are both different checksum even though they are reading the same file. Im using Adler32 but CRC32 also gives me two different checksums (the one on console is always different to the one stored in checksum.txt) and im not sure whats causing it frankly :/
Here is the code that takes userinput and generates the checksum file:
package attemp2;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
public class main {
public static void main(String[] args) throws IOException {
System.out.println("All inputs will be recorded into a sigle file. Enter 'x' when done. A checksum File will aslo be created at the end");
FileWriter fw = new FileWriter("d:/input.txt", false); // clears previous entry in file.
while (true) {
Scanner input = new Scanner(System.in); //get user input
String ch = input.nextLine(); //stores user input
System.out.println(ch); //prints out what user just inputed
if (ch.equals("x")) { //stops running if 'x' is entered
break;
}
BufferedWriter writer = new BufferedWriter(new FileWriter("d:/input.txt", true));
writer.write(ch);
writer.newLine(); // Add new line
writer.close();
}
try {
FileReader reader = new FileReader("d:/input.txt");
BufferedReader br = new BufferedReader(reader);
// read line by line String line;
String read = "";
String line;
while ((line = br.readLine()) != null) {
read = read + line;
//prints out text in file currently
System.out.println(line);
}
//checksum.txt generation
byte buffer[] = read.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
CheckedInputStream cis = new CheckedInputStream(bais, new Adler32());
byte readBuffer[] = new byte[buffer.length];
cis.read(readBuffer);
FileOutputStream out = new FileOutputStream("d://checksum.txt");
BufferedWriter wrt = new BufferedWriter(new FileWriter("d:/checksum.txt", false));
wrt.write(Long.toString(cis.getChecksum().getValue()));
wrt.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
The code that reads the file and generates a checksum in console:
package check;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;
import java.util.zip.Adler32;
public class CheckSum {
private Adler32 checksum;
private String filepath;
InputStream inputStream;
public CheckSum(String filepath) throws FileNotFoundException{
this.filepath = filepath;
checksum = new Adler32();
inputStream = new FileInputStream(filepath);
}
public long generateChecksum() throws IOException{
int c;
while((c = inputStream.read())!=-1){
checksum.update(c);
}
return checksum.getValue();
}
public void read() throws IOException{
File file = new File(filepath);
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null) {
System.out.println(st);
}
}
public static void main(String[] args) throws Exception{
Scanner scanner = new Scanner(System.in);
String filepath = "d:/input.txt";
CheckSum checksum = new CheckSum(filepath);
checksum.read();
System.out.println("For the file: "+filepath);
System.out.println("The checksum generated is: "+checksum.generateChecksum());
}
}
Please learn how to use a debugger, see What is a debugger and how can it help me diagnose problems?.
That being said, you have some problems with your code. First you are calculating the checksum on an empty array. When you write:
byte readBuffer[] = new byte[buffer.length];
cis.read(readBuffer);
you are reading an empty array of the size of the buffer array. You don't need to create a new array. In fact, you should read the buffer array you already have, since there is your content. In this case you just write:
cis.read(buffer);
The next problem is that you are using readers and writers, which are used for text/string files, but checksum/hash algorithm usually works on byte level. This can result in several errors like encoding (ASCII, UTF-8, etc. stuff) and line-termination issues (\n vs. \r\n vs. \r).
However, in this case you are working with readLine(). This method does not return the line-termination at the end, see the documentation of readLine():
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
So, what you are reading from the file is not the same what is actually in the file. But your CheckSum class reads every byte in the saved file (as it should). Assume you enter only the string "abc". Your first calculation will be run on the 3 bytes long array with the values:
[97,98,99]
The line-termination is ignored by the readLine() method, but it is still present in the file. When you check the checksum with the second program, the InputStream you are using will see the following bytes:
[97,98,99,10]
(The bytes at the end depends on the OS you are using)
As you see you run the checksum on different byte arrays, resulting in different checksum values. So, make sure you are running the checksum checks on the same byte array content (or InputStream content) to get the same checksum in both applications.
Related
I was searching on the internet and came across this code to read files from a file and convert it into a string. But I don't understand how in.read(arr) is reading all the contents of a file at once.
import java.util.Scanner;
import java.io.*;
class Main{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String s = sc.next();
try
{
File file = new File(s);
BufferedReader in = new BufferedReader(new FileReader(file));
int c;
char arr[] = new char[(int)file.length()];
in.read(arr);
String res = new String(arr);
System.out.println(res);
}
catch(Exception e){}
}
}
In contemporary Java code you would use Files.readString for this purpose. It was introduced with Java 11, and it specifically reads an entire text file into a String.
What happens in the code you ask about is a common thing with read(someArray) methods: they read up to a number of conditions, something like
The specified number of characters have been read,
The read method of the underlying stream returns -1, indicating end-of-file, or
The ready method of the underlying stream returns false, indicating that further input requests would block.
here you use the first and second conditions, and hope that the third one will not kick in, so reading from a local file won't cause "blocking" at an arbitrary file position.
File.length tells you the size of the file in bytes. There can't be more characters in a file than its size in bytes, that's why file.length is a good upper estimate for the number of characters you would need. However as there are encodings which can result a single character stored as multiple bytes (such as UTF-8), you should actually use the return value of that read() call which tells you the number of characters read, and then pass it to the String constructor:
char arr[] = new char[(int)file.length()];
int count = in.read(arr);
String res = new String(arr, 0, count);
Actual variants of your code:
with File.toPath() if you like
import java.util.Scanner;
import java.io.File;
import java.nio.file.Files; // "Files" is needed of course
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String s = sc.next();
try
{
File file = new File(s);
String res = Files.readString(file.toPath()); // many lines disappeared
System.out.println(res);
}
catch (Exception e) {}
}
}
with java.nio classes and interfaces:
import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String s = sc.next();
try
{
Path path = Paths.get(s); // "File file" became "Path path"
String res = Files.readString(path); // many lines disappeared again
System.out.println(res);
}
catch (Exception e) {}
}
}
It's up to a bit of taste and what you have already. If you have a File object anyway (like you need its size for some another purpose, or you get it from GUI code for example - in many cases that's going to be a File), use its toPath(). If you have a String, a single Paths.get() is less typing.
The object 'in' has the information related to file.
'arr' is the array whose length is equal to the length of file contents.
Then 'in' is read upto that length of file
The assignment is to read a file, create a new file that matches the input file but has numbers lines added
I have several examples to copy from. I have tried new File(), new FileReader() and BufferedReader(). I can't seem to get any data out of the input file
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;
public class Hw1_43 {
public static void main(String[] args) throws FileNotFoundException
{
// Prompt user for input file name
Scanner in = new Scanner(System.in);
System.out.print("Enter input file name: ");
String inputFileName = in.next(); // instantiate input file name for later use
Scanner inputFile = new Scanner(new FileReader(inputFileName));
//ArrayList<String> line = new ArrayList();
int counter = 0;
while (inputFile.hasNextLine()) {
String line = inputFile.nextLine();
System.out.println(counter + line);
counter ++;
}
System.out.println(inputFileName);
}
}
Also after I get the input file to read and write it into an output file, where is the output file so I can look at it to make sure it is correct?
FileReader only reads from a file. You need to create a FileWriter, which writes to a file (e.g. FileWriter outputFile = new FileWriter ("C:/tmp/output_file.txt")). As you read from the FileWriter, prepend the line numbers to each line, then write to the FileWriter.
I recommend using BufferedReader instead of Scanner. You can then use then uses the .readLine() or .lines() methods, the latter can be streamed into a BufferedWriter.
I have a data file with the entries (5 numbers per line:
8,1,3,7,9
2,4,7,10,11
9,99,4,7,3
5,1,2,3,17
The complete data file is extremely large but has similar data throughout.
I am trying to read the data line by line from the input file,
Sort the numbers on that line,
Then write the sorted numbers to a new output file.
Rather than storing the entire input file into an array or memory, I want to only store the numbers for each line, one line at a time, sort it, and write those sorted numbers to the next line in the output file. I have come up with an initial way to read and write the smaller data set to a different file, but its writing the array which appears within braces such as [ 1, 3, 7, 8, 9] instead of as 1, 3, 7, 8, 9 . When I figure out what the correct, more efficient way is, I was also planning on creating a seperate class just for the sorting and would read one line at a time from the data file, pass it to the seperate class which would sort it and write it to the output datafile. How can I correctly input each line one at a time into the array, sort it, and write it to the new output datafile?
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Soup {
public static void main(String[] args) throws IOException {
File dir = new File(".");
String source = dir.getCanonicalPath() + File.separator + "Code.txt";
String dest = dir.getCanonicalPath() + File.separator + "Dest.txt";
File fin = new File(source);
FileInputStream fis = new FileInputStream(fin);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
FileWriter fstream = new FileWriter(dest, true);
BufferedWriter out = new BufferedWriter(fstream);
String aLine = null;
while ((aLine = in.readLine()) != null) {
//Process each line and add output to Dest.txt file
//Arrays.sort(aLine);
String[] strings = aLine.split(",");
int[] numbers = new int[strings.length];
for (int i=0; i < numbers.length; i++)
{
numbers[i] = Integer.parseInt(strings[i]);
}
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
//for(int k=0; k < numbers.length; k++)
//out.writeObject(numbers[k]);
//out.write(aLine);
out.write(Arrays.toString(numbers));
//out.write(numbers
//out.write((numbers));
out.newLine();
}
// do not forget to close the buffer reader
in.close();
// close buffer writer
out.close();
Your Arrays.toString(numbers) adds the brackets. Just replace the brackets with an empty string before you write it to the file -
out.write(Arrays.toString(numbers).replaceAll("\[|\]", ""));
out.write(Arrays.toString(numbers).replaceAll("\[|\]", "")); does not work, error: at java.lang.String.replaceAll(Unknown Source)
Also, I am trying to figure out a better method of performing the sort and write task, Im sure my code is not efficient and am looking for a more efficient way to accomplish the task. I am open to suggestions.
i had a input file having 45311 instance. after applying my programing task. when i m write it back in new file it actually write 43371 instance only.it is run successfully but where are my remaining instances.
package kmean;
//package greenblocks.statistics;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import weka.clusterers.SimpleKMeans;
import weka.core.Instances;
/**
*
* #author admin
*/
public class Kmean {
public static BufferedReader readDataFile(String filename) {
BufferedReader inputReader = null;
try {
inputReader = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException ex) {
System.err.println("File not found: " + filename);
}
return inputReader;
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws IOException, Exception {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("perturbed1.csv"));
}
catch (IOException e) {
}
SimpleKMeans kmeans = new SimpleKMeans();
kmeans.setSeed(10);
//important parameter to set: preserver order, number of cluster.
kmeans.setPreserveInstancesOrder(true);
kmeans.setNumClusters(5);
BufferedReader datafile = readDataFile("elecNormNew.arff");
// BufferedReader datafile = readDataFile("perturbed.csv");
Instances data = new Instances(datafile);
kmeans.buildClusterer(data);
// This array returns the cluster number (starting with 0) for each instance
// The array has as many elements as the number of instances
int[] assignments = kmeans.getAssignments();
StringBuilder sb = new StringBuilder();
int i=0;
for(int clusterNum : assignments) {
// System.out.printf("Instance %d -> Cluster %d \n", i, clusterNum);
sb.append(i);
sb.append(";");
sb.append(clusterNum);
sb.append("\n");
//System.out.printf("\n");
i++;
}
System.out.println(sb.toString());
writer.write(sb.toString()+"\n");
// TODO code application logic here
}
}
The neat fact about buffered file writers are, that they take your input and keep it, until the buffer is full. This reduces the i/o operations. At best one write operation fits into one hdd write buffer so the operating system take the whole buffer as one i/o command. The downside is that if at the end if you do not flush() the buffer, the rest of the content will not be written to disk. If you call close() any pending bytes will be written and the resources be freed. In java 7 and above you can use the autoclosing feature by just opening the stream in your try statement:
try(Inputstream is = new ...) {
If you may have any data to write after your code, you can use .flush() to ensure the data is written.
The buffer size is set by default to 8k, but this may wary from jre and version.
You should call writer.close() at the end after writing all data.
insted of writer.write(sb.toString()+"\n");
try writer.write(sb.toString()+writer.newLine());
and finish your writig progress with a
writer.flush();
writer.close();
had some problems myself with "\n" maby thats the problem.
I'm trying to write some text to a file. I have a while loop that is supposed to just take some text and write the exact same text back to the file.
I discovered that the while loop is never entered because Scanner thinks there's no more text to read. But there is.
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class WriteToFile {
public static void main(String[] args) throws FileNotFoundException {
String whatToWrite = "";
File theFile = new File("C:\\test.txt");
Scanner readinput = new Scanner(theFile);
PrintWriter output = new PrintWriter(theFile);
while (readinput.hasNext()) { //why is this false initially?
String whatToRead = readinput.next();
whatToWrite = whatToRead;
output.print(whatToWrite);
}
readinput.close();
output.close();
}
}
The text file just contains random words. Dog, cat, etc.
When I run the code, text.txt becomes empty.
There was a similar question: https://stackoverflow.com/questions/8495850/scanner-hasnext-returns-false which pointed to encoding issues. I use Windows 7 and U.S. language. Can I find out how the text file is encoded somehow?
Update:
Indeed, as Ph.Voronov commented, the PrintWriter line erases the file contents! user2115021 is right, if you use PrintWriter you should not work on one file. Unfortunately, for the assignment I had to solve, I had to work with a single file. Here's what I did:
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class WriteToFile {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<String> theWords = new ArrayList<String>();
File theFile = new File("C:\\test.txt");
Scanner readinput = new Scanner(theFile);
while (readinput.hasNext()) {
theWords.add(readinput.next());
}
readinput.close();
PrintWriter output = new PrintWriter(theFile); //we already got all of
//the file content, so it's safe to erase it now
for (int a = 0; a < theWords.size(); a++) {
output.print(theWords.get(a));
if (a != theWords.size() - 1) {
output.print(" ");
}
}
output.close();
}
}
PrintWriter output = new PrintWriter(theFile);
It erases your file.
You are trying to read the file using SCANNER and writing to another file using PRINTWRITER,but both are working on same file.PRINTWRITER clear the content of the file to write the content.Both the class need to work on different file.