GZIP Input Stream strips trailing commas in Java - java

I was trying to read a CSV file in Java. It had been compressed by GZIP and I used the inbuilt GZIPInputStream to read the CSV file. The piece of code being used to read the file was as follows:
BufferedReader br = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream("xyz.csv.gz"))));
Some days later, I compressed 4 files using the zip format and they were in a folder named xyz.zip. Note: The 4 files were not individually compressed. This files was being read in the following manner:
InputStream is = getClass().getResourceAsStream("xyz.zip");
ZipInputStream zipIs = new ZipInputStream(is);
ZipEntry ze = null;
while ((ze = zipIs.getNextEntry()) != null) {
String zipEntryFilename = ze.getName();
BufferedReader br = new BufferedReader(new InputStreamReader(zipIs));
while ((line = br.readLine()) != null) {
//assume that a csv file is being read.
//In this format, trailing commas are not removed.
}
One difference I discovered in these two methods was that GZIP stripped trailing commas in the csv files, while the same did not happen in the other case.
The two pieces of code given above show how I am using the InputStream to read the files of my interest.
What is the reason behind this behavior? Is this a bug?

Related

Write csv file from byte[]

I am getting a file in byte[] which have comma separated values in quotes, I want to save it as CSV by using OpenCSV. I am using this to save it in CSV format.
Following is my code to convert byte[] to array and then store it in file
byte[] bytes = myByteStream.getFile();
String decoded = new String(bytes, "UTF-8");
//String lines[] = decoded.split("\\r?\\n");
FileOutputStream fos = new FileOutputStream("/home/myPC/Desktop/test.csv");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
CSVWriter writer = new CSVWriter(osw);
String[] row = {decoded};
writer.writeNext(row);
writer.close();
osw.close();
But this above code puts extra quotes around and also merge all lines in one line.
Any help on how to do this properly ?
You can prevent adding quotes to the cell values within the constructor of CSVWriter, for example:
CSVWriter writer = new CSVWriter(osw, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
Regarding the whole byte array persisted as a single row. Are you sure there are newlines within the original file.
If so you might get away by doing the following:
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "ASCII"));
String line;
while ((line = reader.readLine()) != null) {
// Handle the line, ideally in a separate method
}
Got this from splitting a byte array on a particular byte
I think Apache Commons CSV is a better CSV library. From the pasted code it is not clear if you want to somehow change the contents of the file or just duplicate it: in the latter case you don't even need to parse the file as CSV records - just copy it byte-for-byte. In the former case, ie if you need to modify the content of the file, you need something like (Commons CSV used here)
CSVParser parser = CSVFormat.newFormat(',').parse(
new InputStreamReader(new ByteArrayInputStream(bytes), "UTF8"));
CSVPrinter printer = CSVFormat.newFormat(',').print(out);
for (CSVRecord record : parser) {
try {
printer.printRecord(record);
} catch (Exception e) {
throw new RuntimeException("Error at line "
+ parser.getCurrentLineNumber(), e);
}
}
parser.close();
printer.close();
Look at the Javadoc for CSVFormat

How to export Excel CSV with utf-8 and read into Java?

I have excel spreadsheet which contain many "Chinese(zh-TW)" & "English(en-US)" word together and this data need to import to a database table with utf-8 encoding.
My first try was use Excel "Save-As" function to export data as CSV. However, the data was not able to read correctly since Excel is not allow to change the character encoding for "Save-AS" function.
After searching, I found the easy way to complete the task is following below steps:
Save the Excel sheet as "Unicode Text (.txt)" (Excel encode in UTF16 and use TAB to delimited each column.
Below is Java code snippet to read the content of this Excel unicode exported file:
File dataFile = new File("my-unicode-file-exported.txt"));
InputStreamReader isr = new InputStreamReader(new FileInputStream(dataFile), "UTF-16");
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
// splits by TAB
String elements = line.split("\t");
// process each element
String element_01 = elements[0];
String element_02 = elements[1];
.....
}
br.close();

Java read file and send in the server

I need to read contents of a file as a server, and then send the read data file, for the client so the client print it out on the Client terminal.
The problem is that I can't find a way or method to read a txt file from the current directory which my java file and txt file are existed.
Please help me.
There are many ways to read text file or file in java. It depend on you to that in which format you need to pass your file content to client side.
Here are some method to reading file in java.
1. Using BufferedReader class
BufferedReader input = new BufferedReader(new FileReader(aFile));
String line = null; //not declared within while loop
while (( line = input.readLine()) != null){
String curLine = line;
//Process line
}
2.Using Apache Common IOUtils with the class IOUtils.toString() method.
FileInputStream inputStream = new FileInputStream("FILEPATH/FILENAME");
try {
String everything = IOUtils.toString(inputStream);
} finally {
inputStream.close();
}
3.Using the Scanner class in Java and the FileReader
Scanner in = new Scanner(new FileReader("FILENAME/FILEPATH"));
while (scanner.hasNextLine()){
//process each line in some way
String line = scanner.nextLine();
}
Scanner has several methods for reading in strings, numbers, etc...
4.In JAVA 7 this is the best way to simply read a textfile
new String(Files.readAllBytes(...))
or Files.readAllLines(...)
Path path = Paths.get("FILENAME");
List<String> allLines = Files.readAllLines(path, ENCODING);
Please refer this link for more onfomation.
You can use BufferedReader to read from a txt file.
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line = br.readLine();
here fileName is a string that contain your absolute file name.
eg : fileName = "C:\temp\test.txt";
You can read file by using BufferedReader.
File file=new File("filepath");
BufferedReader br=new BufferedReader(new FileReader(file)); //Here you create an object of bufferedreader which file read through filereader
String data=br.readLine();
while(data!=null)
{
System.out.println(data); // Writing in the console
data=br.readLine();
}
This will taking input from file and giving output to console.If you want it write in other file then use BufferedWriter.
File out=new File("outputfilepath");
BufferedWriter bw=new BufferedWriter(new FileWriter(out));
simply us bw.write() instead of System.out.println();.

Replace Line in File

I have a text file which I have a BufferedReader read from.
String sCurrentLine;
File myFile = new File("/sdcard/file.txt");
BufferedReader buf = new BufferedReader(new FileReader(myFile));
while ((sCurrentLine = buf.readLine()) != null) {
}
What I want to do is read a specific line and then replace it with something else whilst leaving the rest of the file alone. How should I do this?
Create a temporary file
Read through the file file.txt, write the output to the temporary file making the replacement as necessary
Close the file
Delete/backup the original file
Rename the temporary file to the original file

Reading a json file from local path by bufferedReader to construct a html file

I am using following code to write json to my local path which i get from my html page.Again I have to construct a html page by reading content from the saved local json file.For this I have to read this saved file from local which is plain text and give as input to java file. I got confused whether to use Buffered Reader or BufferedInputStream to read that file from local path.Please help me.
java.io.BufferedWriter jsonOut = new java.io.BufferedWriter(
new java.io.OutputStreamWriter(
new java.io.FileOutputStream(uploadDir +
_req.getParameter("filename")), "ISO-8859-1"));
BufferedReader for text.
Reason: http://tutorials.jenkov.com/java-io/bufferedreader.html
You can use BufferedReader for text but you should ensure to use the proper charset in your case (otherwise it defaults to the platform charset)
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(myFile),"ISO-8859-1"));
To read a file you can use the following code
File f = new File("your json file");
BufferedReader buf = new BufferedReader(new FileReader(f));
String line = null;
while ((line = buf.readLine()) != null) {
System.out.println("json file line " + line);
// do your changes
}

Categories

Resources