Error. BufferedWriter repeating lines at end of file - java

I have here an error and dont know what is happening.
My class gets a vector of hashmaps and a rute, and then write that hashmap to a file in that route.
This is the code:
/* Variables de entrada */
Vector vecHm = (Vector) context.getAttribute(sVecHashmap);
String strFileLocation = "" + context.getAttribute(sFileLocation);
// Inicializamos variables
FileWriter fileWriter = null;
BufferedWriter bufferedWriter = null;
try
{
fileWriter = new FileWriter(strFileLocation,true);
bufferedWriter = new BufferedWriter(fileWriter);
String linea = "";
String lineaCabecera = "";
for (int i=0;i<vecHm.size();i++)
{
HashMap hm = (LinkedHashMap) vecHm.get(i);
Iterator it = hm.entrySet().iterator();
linea = "";
while (it.hasNext())
{
Map.Entry pairs = (Map.Entry)it.next();
if (i==0)
{
if (lineaCabecera.equals("") == false)
{
lineaCabecera = lineaCabecera + ";";
}
lineaCabecera = lineaCabecera + (String)pairs.getKey();
}
if (linea.equals("") == false)
{
linea = linea + ";";
}
linea = linea + (String)pairs.getValue();
//it.remove(); // avoids a ConcurrentModificationException
}
System.out.println("PRF:: HashmapToFile:: Iteracion: " + i + ". Linea: " + linea);
if (i==0)
{
System.out.println("PRF:: Pinto Cabecera. ");
bufferedWriter.write(lineaCabecera);
bufferedWriter.newLine();
//bufferedWriter.write('\n');
}
bufferedWriter.write(linea);
bufferedWriter.newLine();
//bufferedWriter.write('\n');
}
} catch (Exception e)
{
e.printStackTrace();
throw new WFException(" ERROR writing the file");
} finally
{
try
{
// Cerramos el fichero
bufferedWriter.close();
fileWriter.close();
} catch (Exception e)
{
e.printStackTrace();
throw new WFException(" ERROR closing the file");
}
}
I have a trace that show me the line to write in the file:
System.out.println("PRF:: HashmapToFile:: Iteracion: " + i + ". Linea: " + linea);
The log that i see is this (i will put only the last four iterations):
PRF:: HashmapToFile:: Iteracion: 90. Linea: eufekeptuil;null;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo
PRF:: HashmapToFile:: Iteracion: 91. Linea: hwukbzakmfuutrhnfzm;null;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo
PRF:: HashmapToFile:: Iteracion: 92. Linea: Securitas Europe;29-JAN-15;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo
PRF:: HashmapToFile:: Iteracion: 93. Linea: Tarifa New 544;05-FEB-15;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo
But... when i see the file... i have this at the end:
Securitas Europe;29-JAN-15;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo -OK. Perfect-
Tarifa New 544;05-FEB-15;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo -OK. Perfect-
And then:
N-15;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo (repeated and unfinished line)
Tarifa New 60;15-JAN-15;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo (repeated line)
vjvrqgxavk;null;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo;Inactivo (repeated line)
And another 15 more lines repeated.
Any clue?
Thanks all

Forget the problem. The files are good. The problem is in the downloader. The system is putting more data in that functionality.

Related

Test if strings are equal with Junit returns false even if its egual

I have a method that returns a String and I would like to test if it works correctly. For that I have a test.txt-file that I read and compare to the return value of my method. If I print both Strings out they are exactly the same! Somehow assertEquals still fails.. What am I doing wrong here?
Method to test:
public String statement() {
String result = "Rental Record for " + getName() + "\n";
int frequentRenterPoints = 0;
for (Rental each : this.rentals) {
frequentRenterPoints += each.getFrequentRenterPoints();
// show figures for this rental
result += "\t" + each.getMovie().getTitle() + "\t"
+ " (" + each.getMovie().getQuality() + ")"
+ ": "
+ String.valueOf(each.getCharge()) + "\n";
}
// add footer lines
result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n";
result += "You earned " + String.valueOf(frequentRenterPoints)
+ " frequent renter points";
return result;
}
Test:
#Test
public void statementReturnsCorrectlyFormattedString() throws IOException {
// given
customer = new Customer("ElonMusk");
Movie movieOne = new Movie("IronMan1", PriceCodes.REGULAR, Quality.HD);
Movie movieTwo = new Movie("AvengersEndGame", PriceCodes.NEW_RELEASE, Quality.FOUR_K);
Rental rentalOne = new Rental();
rentalOne.setMovie(movieOne);
rentalOne.setDaysRented(5);
Rental rentalTwo = new Rental();
rentalTwo.setMovie(movieTwo);
rentalTwo.setDaysRented(1);
List<Rental> rentalList = new LinkedList<Rental>();
rentalList.add(rentalOne);
rentalList.add(rentalTwo);
customer.setRentals(rentalList);
String expectedString = "";
try {
expectedString = readFile("test.txt");
System.out.println("expected: " + "\n" +expectedString);
} catch (IOException e) {
throw new IOException("Error reading statementTestFile!", e);
}
// when
String statement = customer.statement();
// then
System.out.println("statement: " + "\n" + statement);
System.out.println(expectedString.equals(statement));
assertEquals(expectedString, statement);
}
Output: expectedString
expected:
Rental Record for ElonMusk
IronMan1 (HD): 6.5
AvengersEndGame (FOUR_K): 5.0
Amount owed is 11.5
You earned 2 frequent renter points
Output: statement
statement:
Rental Record for ElonMusk
IronMan1 (HD): 6.5
AvengersEndGame (FOUR_K): 5.0
Amount owed is 11.5
You earned 2 frequent renter points
readFile:
private String readFile(String file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader (file));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = "\n";
try {
while((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
return stringBuilder.toString();
} finally {
reader.close();
}
}
The problem is in the trailing newline you add when reading from a file. You could in trim the string, but what if there were some empty lines at the end of file you wanted to read?
So you can either introduce a 'first line' boolean like that:
boolean isFirstLine = true;
while((line = reader.readLine()) != null) {
if (!isFirstLine) {
stringBuilder.append(ls);
}
stringBuilder.append(line);
isFirstLine = false;
}
Or maybe leave the loop as-is and after it runs delete last character from the builder with:
if (stringBuilder.length() > 0) {
stringBuilder.deleteCharAt(stringBuilder.length() - 1); // or stringBuilder.lastIndexOf("\n");
}
Or do a substring.
Or maybe read the lines into a List collection like ArrayList and later do String.join("\n", linesCollection);.

Java: Create and write in consecutive files

I'm able correctly to create and to write into a new file, but don't in a second one or more.
What's wrong?
int numbOfFile = 0;
PrintWriter bw = new PrintWriter("C:path\test" + numbOfFile + ".csv");
for (Map.Entry<String, List<Integer>> entry : inSorting.entrySet()) {
String key = entry.getKey();
numbOfFile++;
if (!(numbOfFile % 3 == 0)) {
bw.println(key + " ");
} else {
bw.close();
new PrintWriter(""C:path\test" + numbOfFile + ".csv"");
bw.println(key + " ");
}
}
bw.close();

while loop needs to continue

I have this code
static String sCurrentLine = null;
/* keyword */
static String keyword = null;
Scanner keywordFile = null, siteFile = null;
try {
keywordFile = new Scanner(new File("/home/mearts/keywords.txt"));
siteFile = new Scanner(new FileReader(fileChooser.getSelectedFile()));
sCurrentLine = siteFile.nextLine().trim();
keyword = keywordFile.nextLine().trim();
while (sCurrentLine != null){
while (keywordFile.hasNext() || keyword == null) {
System.out.println("Line--> " + keyword);
System.out.println("Current here >>" + sCurrentLine);
if (sCurrentLine.contains(keyword)) {
System.out.println("Found it-->> " + keyword);
keyword = keywordFile.nextLine();
System.out.println("next keyword " + keyword);
///* reset search to top of site file */
siteFile = new Scanner(new
FileReader(fileChooser.getSelectedFile()));
sCurrentLine = siteFile.nextLine().trim();
}
else {
sCurrentLine = siteFile.nextLine();
if (sCurrentLine == null) {
break;
}
if (!sCurrentLine.matches(keyword)){
System.out.println("The following keyword " + keyword + " does not exist in file "
+ fileChooser.getSelectedFile());
}
}
} //2nd while loop
}
}
catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
siteFile.close();
keywordFile.close();
}
and i have a text file called keywords which has a list of keywords in it,
but my logic is off an I cannot figure out why.
I think I may need to run the loop one last time but not sure how to do that
My issue is that the last word in the keyword file never gets read in. so the program stops at the 2nd to last element in the text file.
I am not sure that I understand what your code should do.
If I understood correctly your code, your task is to read keywords from a file with keywords and then find all keywords in another file. Is it correct?
You should separate reading keywords from the file and search for them in the file. You should 'load' keywords in a list and then search through the file.
To load keywords in list
keywordFile = new Scanner(new File("/home/mearts/keywords.txt"));
List<String> keywordsList = new ArrayList<>();
while (keywordFile.hasNextLine()) {
keywordsList.add(keywordFile.nextLine());
}
And to search for keywords in the file
siteFile = new Scanner((Readable) new FileReader(fileChooser.getSelectedFile()));
while (siteFile.hasNextLine()) {
String sCurrentLine = siteFile.nextLine().trim();
for (String keyword : keywordsList) {
if (sCurrentLine.contains(keyword)) {
System.out.println("Found it-->> " + keyword);
break;
}
}
System.out.println(
"The following keyword " + keyword + " does not exist in file " + fileChooser.getSelectedFile());
}
I hope this will help :)

Java CSVReader ignore commas in double quotes

I have a CSV file that I am having trouble parsing. I am using the opencsv library. Here is what my data looks like and what I am trying to achieve.
RPT_PE,CLASS,RPT_MKT,PROV_CTRCT,CENTER_NM,GK_TY,MBR_NM,MBR_PID
"20150801","NULL","33612","00083249P PCP602","JOE SMITH ARNP","NULL","FRANK, LUCAS E","50004655200"
The issue I am having is the member name ("FRANK, LUCAS E") is being split into two columns and the member name should be one. Again I'm using opencsv and a comma as the separator. Is there any way I can ignore the commas inside the double-quotes?
public void loadCSV(String csvFile, String tableName,
boolean truncateBeforeLoad) throws Exception {
CSVReader csvReader = null;
if (null == this.connection) {
throw new Exception("Not a valid connection.");
}
try {
csvReader = new CSVReader(new FileReader(csvFile), this.seprator);
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Error occured while executing file. "
+ e.getMessage());
}
String[] headerRow = csvReader.readNext();
if (null == headerRow) {
throw new FileNotFoundException(
"No columns defined in given CSV file."
+ "Please check the CSV file format.");
}
String questionmarks = StringUtils.repeat("?,", headerRow.length);
questionmarks = (String) questionmarks.subSequence(0, questionmarks
.length() - 1);
String query = SQL_INSERT.replaceFirst(TABLE_REGEX, tableName);
System.out.println("Base Query: " + query);
String headerRowMod = Arrays.toString(headerRow).replaceAll(", ]", "]");
String[] strArray = headerRowMod.split(",");
query = query
.replaceFirst(KEYS_REGEX, StringUtils.join(strArray, ","));
System.out.println("Add Headers: " + query);
query = query.replaceFirst(VALUES_REGEX, questionmarks);
System.out.println("Add questionmarks: " + query);
String[] nextLine;
Connection con = null;
PreparedStatement ps = null;
try {
con = this.connection;
con.setAutoCommit(false);
ps = con.prepareStatement(query);
if (truncateBeforeLoad) {
//delete data from table before loading csv
con.createStatement().execute("DELETE FROM " + tableName);
}
final int batchSize = 1000;
int count = 0;
Date date = null;
while ((nextLine = csvReader.readNext()) != null) {
System.out.println("Next Line: " + Arrays.toString(nextLine));
if (null != nextLine) {
int index = 1;
for (String string : nextLine) {
date = DateUtil.convertToDate(string);
if (null != date) {
ps.setDate(index++, new java.sql.Date(date
.getTime()));
} else {
ps.setString(index++, string);
}
}
ps.addBatch();
}
if (++count % batchSize == 0) {
ps.executeBatch();
}
}
ps.executeBatch(); // insert remaining records
con.commit();
} catch (SQLException | IOException e) {
con.rollback();
e.printStackTrace();
throw new Exception(
"Error occured while loading data from file to database."
+ e.getMessage());
} finally {
if (null != ps) {
ps.close();
}
if (null != con) {
con.close();
}
csvReader.close();
}
}
public char getSeprator() {
return seprator;
}
public void setSeprator(char seprator) {
this.seprator = seprator;
}
public char getQuoteChar() {
return quoteChar;
}
public void setQuoteChar(char quoteChar) {
this.quoteChar = quoteChar;
}
}
Did you try the the following?
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), ',');
I wrote a following program and it works for me, I got the following result:
[20150801] [NULL] [33612] [00083249P PCP602] [JOE SMITH ARNP] [NULL]
[FRANK, LUCAS E] [50004655200]
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import au.com.bytecode.opencsv.CSVReader;
public class CVSTest {
/**
* #param args
*/
public static void main(String[] args) {
CSVReader reader = null;
try {
reader = new CSVReader(new FileReader(
"C:/Work/Dev/Projects/Pure_Test/Test/src/cvs"), ',');
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String[] nextLine;
try {
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println("[" + nextLine[0] + "] [" + nextLine[1]
+ "] [" + nextLine[2] + "] [" + nextLine[3] + "] ["
+ nextLine[4] + "] [" + nextLine[5] + "] ["
+ nextLine[6] + "] [" + nextLine[7] + "]");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
According to the documentation, you can supply custom separator and quote characters in the constructor, which should deal with it:
CSVReader(Reader reader, char separator, char quotechar)
Construct your reader with , as separator and " as quotechar.
It is simple to load your CSV as an SQL table into HSQLDB, then select rows from the table to insert into another database. HSQLDB handles commas inside quotes. You need to define your text source as "quoted". See this:
http://hsqldb.org/doc/2.0/guide/texttables-chapt.html
Your case should be handled out of the box with no special configuration required.
If you can't make it work, then just switch to uniVocity-parsers to do this for you - it's twice as fast in comparison to OpenCSV, requires much less code and is packed with features.
CsvParserSettings settings = new CsvParserSettings(); // you have many configuration options here - check the tutorial.
CsvParser parser = new CsvParser(settings);
List<String[]> allRows = parser.parseAll(new FileReader(new File("C:/Work/Dev/Projects/Pure_Test/Test/src/cvs")));
Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).

Trouble Writing to file in java

Hey im trying to write to a file but im getting an error on the line where it writes the line that goes into each line of a text file, cant figure it out any help would be greatly appreciated
The Writer Code
.
public static void stuIDWrite() throws IOException
{
Writer writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("Res/stuIDSorted.txt")));
} catch (IOException ex) {
// report
} finally {
try {writer.close();} catch (Exception ex) {}
}
int i = 0;
while (i <= stuArrayIdSort.length + 1)
{
ln = stuArrayIdSort[i].getStuLastName();
fn = stuArrayIdSort[i].getStuFirstName();
pn = stuArrayIdSort[i].getStuFirstName();
id = stuArrayIdSort[i].getStuId();
ft = stuArrayIdSort[i].getFTime();
phn =stuArrayIdSort[i].getPhoneNum();
lj = stuArrayIdSort[i].getLovJava();
con = stuArrayIdSort[i].getCont();
writer.write(ln + "," + fn + "," + pn + ","+ id + "," + ft + "," + phn + "," + lj + "," + con + "\n");
writer.close();
i++;
}
The Full Code
import java.io.*;
import java.util.*;
public class StudentMain {
/**
* #param args
*/
//array and sorting variables
public static studentConstructor[] stuArrayOrig = new studentConstructor[23];
private static studentConstructor[] stuArrayIdSort = new studentConstructor[23];
private static studentConstructor[] stuArrayNameSort = new studentConstructor[23];
private static int lineCount = 0;
private static int nElms = 0;
//writer
//studentConstructor variables
public static String fn; //First Name
public static String ln; //Last Name
public static String pn; //Preferred Name
public static int id; //Student Id Number
public static boolean ft;//Full-time Boolean
public static int phn; //Student Phone Number
public static boolean lj;//Loving java Boolean
public static String con;//Continuing
File idSort = new File("stuListSortID.txt");
public static void StuRead()
{
Scanner inFile = null;
try
{
inFile = new Scanner
(new FileReader("Res/students.txt"));
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
System.out.println("File Not Found");
e.printStackTrace();
}
while (inFile.hasNextLine()){
inFile.useDelimiter(",|\\n"); //breaks the lines into single info
ln = inFile.next();
System.out.println(ln);
fn = inFile.next();
System.out.println(fn);
pn = inFile.next();
System.out.println(pn);
id = inFile.nextInt();
System.out.println(id);
ft = inFile.nextBoolean();
System.out.println(ft);
phn = inFile.nextInt();
System.out.println(phn);
lj = inFile.nextBoolean();
System.out.println(lj);
con = inFile.next();
System.out.println(con);
studentConstructor st = new studentConstructor(ln, fn, pn, id, ft, phn, lj, con);
stuArrayOrig[lineCount] = st;
inFile.nextLine();
System.out.println(stuArrayOrig[lineCount]);
lineCount++;
}
//setting info into other arrays
stuArrayIdSort = stuArrayOrig;
stuArrayNameSort = stuArrayOrig;
System.out.println("orig array length" + stuArrayOrig.length);
System.out.println("id array length" + stuArrayIdSort.length);
System.out.println("name array length" + stuArrayNameSort.length);
System.out.println("number of file lines" + lineCount);
inFile.close();
}
public static void stuIdSort()
{
studentConstructor temp;
boolean sorted = false;
while (sorted == false)
{ sorted=true;
for (int i=0; i<stuArrayIdSort.length-1 ; i++)
{
if(stuArrayIdSort[i].getStuId() > stuArrayIdSort[i+1].getStuId())
{
temp = stuArrayIdSort[i+1];
stuArrayIdSort[i+1] = stuArrayIdSort[i];
stuArrayIdSort[i] = temp;
sorted=false;
}
}
}
for(int i=0; i<stuArrayIdSort.length; i++)
{
int getSC = stuArrayIdSort[i].studentId;
System.out.println("number of swaps " + i+1 +" " +getSC);
}
}
//stuArrayIdSort[i].getStuLastName(),stuArrayIdSort[i].getStuFirstName(),stuArrayIdSort[i].getPrefName(),stuArrayIdSort[i].getStuId(),stuArrayIdSort[i].getFTime(),stuArrayIdSort[i].getPhoneNum(),stuArrayIdSort[i].getLovJava(),stuArrayIdSort[i].getCont()
public static void stuIDWrite() throws IOException
{
Writer writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("Res/stuIDSorted.txt")));
} catch (IOException ex) {
// report
} finally {
try {writer.close();} catch (Exception ex) {}
}
int i = 0;
while (i <= stuArrayIdSort.length + 1)
{
ln = stuArrayIdSort[i].getStuLastName();
fn = stuArrayIdSort[i].getStuFirstName();
pn = stuArrayIdSort[i].getStuFirstName();
id = stuArrayIdSort[i].getStuId();
ft = stuArrayIdSort[i].getFTime();
phn =stuArrayIdSort[i].getPhoneNum();
lj = stuArrayIdSort[i].getLovJava();
con = stuArrayIdSort[i].getCont();
writer.write(ln + "," + fn + "," + pn + ","+ id + "," + ft + "," + phn + "," + lj + "," + con + "\n");
writer.close();
i++;
}
}
public static void stuNameSort()
{
}
public static void stuNameWrire()
{
}
}
//lastName, firstName, perName, studentId, fulltime,
Ok, here is what you should do:
What's happening is that you are closing it before it can actually do anything. So, lets move your finally clause to the end of everything:
public static void stuIDWrite() throws IOException
{
Writer writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("Res/stuIDSorted.txt")));
int i = 0;
while (i <= stuArrayIdSort.length + 1)
{
ln = stuArrayIdSort[i].getStuLastName();
fn = stuArrayIdSort[i].getStuFirstName();
pn = stuArrayIdSort[i].getStuFirstName();
id = stuArrayIdSort[i].getStuId();
ft = stuArrayIdSort[i].getFTime();
phn =stuArrayIdSort[i].getPhoneNum();
lj = stuArrayIdSort[i].getLovJava();
con = stuArrayIdSort[i].getCont();
writer.write(ln + "," + fn + "," + pn + ","+ id + "," + ft + "," + phn + "," + lj + "," + con + "\n");
i++;
}
} catch (IOException ex) {
// report
} finally {
try {writer.close();} catch (Exception ex) {}
}
I'm not sure you understand how try...catch...finally works. Here's what you have:
Writer writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("Res/stuIDSorted.txt")));
} catch (IOException ex) {
// report
} finally {
>>>>>>> **try {writer.close();} catch (Exception ex) {}**
}
int i = 0;
while (i <= stuArrayIdSort.length + 1)
{
//bunch of stuff
writer.write(...);
>>>>>>> **writer.close();**
i++;
}
}
You close writer ONCE before you've even used it (finally block gets executed after the try block), and ONCE inside the loop. So, if somehow the code could make it past the writer.close() in the finally block, it would never make it through the loop more than once.
It is not necessary to close a BufferedWriter. The class makes sure to close it internally.
If you are using Java 7, you might want to consider using the "try-with-resources" syntax, which can simplify a correct implementation of file handling greatly in cases like this. Your original code had some issues, but even the accepted answer has some problems that I believe will result in a NullPointerException in the case where the file can't be opened (unverified).
I think you may also have some problems with your while loop boundary conditions also. I've changed the while loop to the more traditional for loop. Keep in mind that java array elements run from 0 to array.length - 1 inclusive.
public static void stuIDWrite() throws IOException
{
try (FileWriter writer = new FileWriter("Res/stuIDSorted.txt"))
{
for (int i = 0; i < stuArrayIdSort.length; ++i)
{
ln = stuArrayIdSort[i].getStuLastName();
fn = stuArrayIdSort[i].getStuFirstName();
pn = stuArrayIdSort[i].getStuFirstName();
id = stuArrayIdSort[i].getStuId();
ft = stuArrayIdSort[i].getFTime();
phn = stuArrayIdSort[i].getPhoneNum();
lj = stuArrayIdSort[i].getLovJava();
con = stuArrayIdSort[i].getCont();
writer.write(ln + "," + fn + "," + pn + "," + id + "," + ft + "," + phn + "," + lj + "," + con + "\n");
}
}
}
You could also look at using the "enhanced for loop" syntax for the inner loop that may further streamline things.

Categories

Resources