CSV file
I am trying to save data separately by using ArrayList.
However, after saving one data into the array, it skips one line then saves the data. Please help me with this problem.
private void readCSVFile(String tickerCode, File file) {
System.out.println("Reading file " + tickerCode);
System.out.println(file.getPath());
try {
ArrayList<List<String>> line = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader(file));
CSVReaderHeaderAware csvReader = new CSVReaderHeaderAware(br);
int numLine = 0;
String[] values;
while ((values = csvReader.readNext()) != null) {
ArrayList<String> data = new ArrayList<>();
data.add(values[0]);
data.add(values[1]);
data.add(values[2]);
data.add(values[3]);
data.add(values[4]);
data.add(values[5]);
data.add(values[6]);
line.add(data);
if(numLine == 0){
System.out.print("StartDate = "+ values[0]);
}
if((csvReader.readNext() == null)){
System.out.println(" EndDate = " + values[0]);
}
++numLine;
}
debugger variable
As mentioned in a comment, you cannot use csvReader.readNext() inside the loop as well to check if the next element is null, because it will actually read the next line. So what you can do is keep the last processed row's date in a variable, and then print it as the EndDate after the while loop is finished.
ArrayList<List<String>> line = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader(file));
CSVReaderHeaderAware csvReader = new CSVReaderHeaderAware(br);
int numLine = 0;
String[] values;
String endDate = null;
while ((values = csvReader.readNext()) != null) {
ArrayList<String> data = new ArrayList<>();
data.add(values[0]);
data.add(values[1]);
data.add(values[2]);
data.add(values[3]);
data.add(values[4]);
data.add(values[5]);
data.add(values[6]);
line.add(data);
if(numLine == 0){
System.out.print("StartDate = "+ values[0]);
}
endDate = values[0];
++numLine;
}
if (endDate != null) { // Need to check null in case your CSV didn't have any rows
System.out.println(" EndDate = " + endDate);
}
Related
I need to parse a text file in the form:
Encanto, 6/101-105, 7/320-322
Flora, 1/2-5
Vista, 7/67-70
WORK ORDER
I know how to parse a .txt file that has lines in the form "name, number" into two separate ArrayLists using the following method:
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line;
while( (line = bufferedReader.readLine()) != null){
allNames.add(line.split(", ")[0]);
allNums.add(Integer.parseInt(line.split(",\\s+")[1]));
}
bufferedReader.close();
Now I must get the Lists to have these items:
ArrayList<String> names = {"Encanto", "Flora", "Vista", "WORK ORDER"};
ArrayList<String> lots = {"Bld.6 101", "Bld.6 102", "Bld.6 103", "Bld.6 104", "Bld.6 105", "Bld.7 320", "Bld.7 321", "Bld.7 322"};
ArrayList<String> lots1 = {"Bld.1 2", "Bld.1 3", "Bld.1 4", "Bld.1 5",};
ArrayList<String> lots2 = {"Bld.7 67", "Bld.7 68", "Bld.7 69", "Bld.7 70"};
Create this Parser class
class Parser {
String parseName(String line) {
String[] blocks = line.split(", ");
if (blocks.length > 0) {
return blocks[0];
}
return null;
}
List<String> getLots(String line) {
List<String> lotList = new ArrayList<>();
String[] blocks = line.split(", ");
for (String block : blocks) {
// I suppose that lot line will always have "/" and "-"
if (block.contains("/") && block.contains("-")) {
lotList.addAll(generateLots(block));
}
}
return lotList;
}
private List<String> generateLots(String block) {
List<String> lots = new ArrayList<>();
String prefix = "Bld.";
String bldNumber = block.substring(0, block.indexOf("/"));
int lowest = Integer.parseInt(block.substring(block.indexOf("/") + 1, block.indexOf("-")));
int highest = Integer.parseInt(block.substring(block.indexOf("-") + 1, block.length()));
for (int i = lowest; i <= highest; i++) {
lots.add(prefix + bldNumber + " " + i);
}
return lots;
}
}
Then use it in your code
// create a new Parser object
Parser parser = new Parser();
// get name
System.out.println(parser.parseName(line));
// get lots
List<String> lotList = parser.getLots(line);
for (String lot : lotList) {
System.out.println(lot);
}
=========================================================
Edit:
To answer your comment, you can create lot lists on the fly. If I refer to your question, it will be like this
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line;
Parser parser = new Parser();
List<String> names = new ArrayList<>();
List<List<String>> lotsList = new ArrayList<>();
while ((line = bufferedReader.readLine()) != null) {
names.add(parser.parseName(line));
lotsList.add(parser.getLots(line));
}
bufferedReader.close();
I am trying to match file value with variable value. But somehow it is not matching. I checked it is reading file and holding value in the variable but not matching. Not sure if I have to use contains function.
int rcdMatch = 0;
String st;
String extdeductamt = "1000";
BufferedReader Br = null;
File objFile = new File(strPlanFile + NewFileNmae);
Br = new BufferedReader(new FileReader(objFile));
List < String > list = new ArrayList < String > ();
LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(objFile));
lineNumberReader.skip(Long.MAX_VALUE);
int lines = lineNumberReader.getLineNumber();
while ((st = Br.readLine()) != null) {
arraylist = st;
String amt = arraylist.substring(0, arraylist.length() - 392);
list.add(amt);
Set < String > unique = new HashSet < String > (list);
for (String key: unique) {
rcdMatch = 0;
if (key.trim().toString().equals(stvar)) {
String adjAmt = arraylist.substring(34, arraylist.length() - 348);
System.out.println("Adjustment Amount 1 is: " + adjAmt);
if (extdeductamt.trim().toString().equals(adjAmt.trim().toString())) {
rcdMatch++;
}
if (!(rcdMatch == 0)) {
System.out.println("PASS Amount is displayed: " + adjAmt);
}
}
break;
}
}
I use Apache Tika to get encoding of file.
FileInputStream fis = new FileInputStream(my_file);
final AutoDetectReader detector = new AutoDetectReader(fis);
fis.close();
System.out.println("Encoding:" + detector.getCharset().toString());
I use Scanner to read values from file.
Scanner scanner = new Scanner(my_file, detector.getCharset().toString());
Map<String, String> values = new HashMap<>();
String line, key = null, value = null;
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (line.contains(":")) {
if (key != null) {
values.put(key, value.trim());
key = null;
value = null;
}
int indexOfColon = line.indexOf(":");
key = line.substring(0, indexOfColon);
value = line.substring(indexOfColon + 1);
} else {
value += " " + line;
}
}
Scanner is unable to read text from files with encoding windows-1252, I get empty string.
UPDATE 2018.11.07.
I have same problem in case of BufferedReader.
Map<String, String> values = new HashMap<>();
String line, key = null, value = null;
FileInputStream is = new FileInputStream(my_file);
InputStreamReader isr = new InputStreamReader(is, getEncoding(my_file));
BufferedReader buffReader = new BufferedReader(isr);
while (buffReader.readLine() != null) {
line = buffReader.readLine();
if (line.contains(":")) {
if (key != null) {
values.put(key, value.trim());
key = null;
value = null;
}
int indexOfColon = line.indexOf(":");
key = line.substring(0, indexOfColon);
value = line.substring(indexOfColon + 1);
} else {
value += " " + line;
}
}
Instead of reading lines, I would try reading characters instead using the following approach:
ByteArrayOutputStream line = new ByteArrayOutputStream();
Scanner scanner = new Scanner(my_file);
while (scanner.hasNextInt()) {
int c = 0;
// read every line
while (c != newline) { // TODO: Check for a newline char
c = scanner.nextInt();
line.write((byte) c);
}
byte[] array = line.toByteArray();
String output = new String(array, "Windows-1252"); // This should do the trick
// We have a string here, do your logic
line.reset();
}
This approach is ugly, but uses new String which has the ability to specify a specific encoding. I did not test or run this code at all, but at least it will show you if any content is actually read properly.
I am trying to compare two .txt files (i.e their contents), but when I execute this code my application goes into an infinite loop. Why?
public int compareFile(String fILE_ONE2, String fILE_TWO2)throws Exception
{
File f1 = new File(fILE_ONE2); //OUTFILE
File f2 = new File(fILE_TWO2); //INPUT
FileReader fR1 = new FileReader(f1);
FileReader fR2 = new FileReader(f2);
BufferedReader reader1 = new BufferedReader(fR1);
BufferedReader reader2 = new BufferedReader(fR2);
String line1 = null;
String line2 = null;
int flag=1;
while ((flag==1) &&((line1 = reader1.readLine()) != null)&&((line2 = reader2.readLine()) != null))
{
if (!line1.equalsIgnoreCase(line2))
flag=0;
else
flag=1;
}
reader1.close();
reader2.close();
return flag;
}
I converted your code into a main program. There is no infinite loop in this code.
I am assuming you are comparing 2 text files of a small-ish size.
import java.io.*;
public class Diff {
public static void main(String[] args) throws FileNotFoundException, IOException {
File f1 = new File(args[0]);// OUTFILE
File f2 = new File(args[1]);// INPUT
FileReader fR1 = new FileReader(f1);
FileReader fR2 = new FileReader(f2);
BufferedReader reader1 = new BufferedReader(fR1);
BufferedReader reader2 = new BufferedReader(fR2);
String line1 = null;
String line2 = null;
int flag = 1;
while ((flag == 1) && ((line1 = reader1.readLine()) != null)
&& ((line2 = reader2.readLine()) != null)) {
if (!line1.equalsIgnoreCase(line2))
flag = 0;
}
reader1.close();
reader2.close();
System.out.println("Flag " + flag);
}
}
I ran it on 2 small different text files. This is the output.
javac Diff.java && java Diff a.txt b.txt
Flag 0
If you think you have an infinite loop, the issue might be elsewhere.
The code looks good, no infinite loops. You can remove irrespective check in the code and can update the code as below:
int flag=1;
while (((line1 = reader1.readLine()) != null)&&((line2 = reader2.readLine()) != null))
{
if (!line1.equalsIgnoreCase(line2))
{
flag=0;
break;
}
}
As the return type of the method is integer than it will return 0 if different and 1 if equal.
Assuming text file inputs, an alternative implementation to the while loop:
while (true) // Continue while there are equal lines
{
line1 = reader1.readLine();
line2 = reader2.readLine();
if (line1 == null) // End of file 1
{
return (line2 == null ? 1 : 0); // Equal only if file 2 also ended
}
else if (line2 == null)
{
return 0; // File 2 ended before file 1, so not equal
}
else if (!line1.equalsIgnoreCase(line2)) // Non-null and different lines
{
return 0;
}
// Non-null and equal lines, continue until the input is exhausted
}
The first else if is not necessary, but it is included for clarity purposes. Otherwise, the above code could be simplified to:
while (true) // Continue while there are equal lines
{
line1 = reader1.readLine();
line2 = reader2.readLine();
if (line1 == null) // End of file 1
{
return (line2 == null ? 1 : 0); // Equal only if file 2 also ended
}
if (!line1.equalsIgnoreCase(line2)) // Different lines, or end of file 2
{
return 0;
}
}
The loop should be placed in a try/finally block, to assure that the readers are closed.
Above method by Jess will fail if file2 is same as file1 but has an extra line at the end.
This should work.
public boolean compareTwoFiles(String file1Path, String file2Path)
throws IOException {
File file1 = new File(file1Path);
File file2 = new File(file2Path);
BufferedReader br1 = new BufferedReader(new FileReader(file1));
BufferedReader br2 = new BufferedReader(new FileReader(file2));
String thisLine = null;
String thatLine = null;
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
while ((thisLine = br1.readLine()) != null) {
list1.add(thisLine);
}
while ((thatLine = br2.readLine()) != null) {
list2.add(thatLine);
}
br1.close();
br2.close();
return list1.equals(list2);
}
if you use java8, the code below to compare file contents
public boolean compareTwoFiles(String file1Path, String file2Path){
Path p1 = Paths.get(file1Path);
Path p1 = Paths.get(file1Path);
try{
List<String> listF1 = Files.readAllLines(p1);
List<String> listF2 = Files.readAllLines(p2);
return listF1.containsAll(listF2);
}catch(IOException ie) {
ie.getMessage();
}
}
I want to write small java program to read data file first field and add seqcution number
Input file:
robert,190 vikign,...
robert,2401 windy,...
robert,1555 oakbrook,...
michell,2524 sprint,...
michell,1245 oakbrrok,...
xyz,2455 xyz drive,....
Output file should be:
robert,190 vikign,...,0
robert,2401 windy,...,1
robert,1555 oakbrook,...,2
michell,2524 sprint,...,0
michell,1245 oakbrrok,...,1
xyz,2455 xyz drive,....,0
Check first field when value change sequction number start back to 0 otherwise add sequction number by 1
here is my code:
public static void createseq(String str) {
try {
BufferedReader br = null;
BufferedWriter bfAllBWP = null;
File folderall = new File("Sort_Data_File_Out");
File[] BFFileall = folderall.listFiles();
for (File file : BFFileall) {
br = new BufferedReader(new FileReader(file));
String bwp = "FinalDataFileOut\\" + str;
bfAllBWP = new BufferedWriter(new FileWriter(bwp));
String line;
line = br.readLine();
String[] actionID = line.split("\\|");
String fullname = actionID[0].trim();
int seq = 0;
String fullnameb;
while ((line = br.readLine()) != null) {
actionID = line.split("\\|");
fullnameb = actionID[0].trim();
if(fullname.equals(fullnameb)) {
seq++;
}
else {
System.out.println(line + "======" + seq + "\n");
seq = 0;
fullname = fullnameb;
}
System.out.println("dshgfsdj "+line + "======" + seq + "\n");
}
}
}
catch(Exception letterproof) {
letterproof.printStackTrace();
}
}
The below code will fix the issue.I have updated the code if you face any pblm plz let me know :
Input :
robert,190 vikign,...
robert,2401 windy,...
robert,1555 oakbrook,...
michell,2524 sprint,...
michell,1245 oakbrrok,...
xyz,2455 xyz drive,....
Code :
public static void createseq() {
try {
File file = new File("d:\\words.txt"); //Hardcoded file for testing locally
BufferedReader br = new BufferedReader(new FileReader(file));
HashMap<String,Integer> counter = new HashMap<String, Integer>();
String line;
while((line = br.readLine())!= null)
{
String[] actionID = line.split(",");
String firstName = actionID[0];
if(counter.containsKey(firstName))
{
counter.put(firstName, counter.get(firstName) + 1);
}
else
{
counter.put(firstName,0);
}
System.out.println(line+" "+counter.get(firstName));
}
br.close();
} catch(Exception letterproof) {
letterproof.printStackTrace();
}
}
Ouput Come :
robert,190 vikign,... 0
robert,2401 windy,... 1
robert,1555 oakbrook,... 2
michell,2524 sprint,... 0
michell,1245 oakbrrok,... 1
xyz,2455 xyz drive,.... 0