Im working on converter. I load file, start reading date from it and creating directories by year, month and day(+ another one dir) in witch ends are those converted text files. Everything is fine while creating those directories but in text files is nothing or only chunk of it.
public static void convertFile(File fileToConvert, File whereToSave, long shift) {
BufferedReader reader = null;
BufferedWriter writer = null;
String oldDate = "";
String newDate = "";
boolean boolDate = true;
try {
for (File file : fileToConvert.listFiles()) {
reader = new BufferedReader(new FileReader(file));
boolean block = true;
String line = "";
int lineCounter = 0;
while ((line = reader.readLine()) != null) {
if (lineCounter==0) {
block = true;
} else {
block = false;
}
line = line.replaceAll("[^0-9-,:+NaN.]", "");
String[] data = line.split(",");
if (block) {
data[0] = data[0].substring(0, 10) + " " + data[0].substring(10);
data[0] = SimulatorForRealData.timeShift(data[0], shift);
// ====================================================================================
newDate = data[0].substring(0, 4) + " " + data[0].substring(5, 7) + " "
+ data[0].substring(8, 10);
String savingIn = SimulatorForRealData.createDirs(whereToSave.toString(),
data[0].substring(0, 4), data[0].substring(5, 7), data[0].substring(8, 10));
File f = new File(savingIn + "\\" + FILE_NAME + ".log");
if (!newDate.equals(oldDate) && boolDate == false) {
writer.close();
boolDate = true;
} else {
oldDate = newDate;
boolDate = false;
}
writer = new BufferedWriter(new FileWriter(f));
// =====================================================================================
writer.write("<in date=\"" + data[0].substring(0, 10) + "T" + data[0].substring(11)
+ "\" t=\"1\" >\n");
writer.write(data[0] + "\n");
writer.write(0 + " " + 0 + " " + 0 + "\n");
for (int x = 0; x <= 10; x++) {
writer.write("NaN" + " ");
}
writer.write("\n");
for (String s : data) {
if (s.equals(data[0])) {
continue;
}
writer.write(s + ";");
}
writer.write("\n");
} else {
for (String s : data) {
writer.write(s + ";");
}
writer.write("\n");
}
lineCounter++;
if (lineCounter == 118) {
lineCounter = 0;
writer.write("</in>\n\n");
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here is method where i perform it. Can someone help me. I tried different "writers" and nothing. I have suspicious that it will be problem in closing file but i dont know for sure.
I think you should close every writer you created, not only last one.
for (File file : fileToConvert.listFiles()) {
reader = new BufferedReader(new FileReader(file));
...
writer = new BufferedWriter(new FileWriter(f));
while ((line = reader.readLine()) != null) {
....
}
writer.close();
}
writer flushes all changes on disk only when buffer is overflowed or it is closed.
I see two main problems:
You create a file every time you read a line. You should put it outside the loop (or loops, if you want only one file)
Always data is written with the same filename. It should have different filenames if you make a file for every file read.
Related
I'm getting a java.lang.OutOfMemoryError: Java heap space. I'm not sure what I'm doing wrong. Here is my code:
StringBuffer finalString = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new FileReader("collegeData 2.txt"));
StringBuffer returnFile = new StringBuffer();
returnFile.append(br.readLine() + br.readLine());
ArrayList<String> allData = new ArrayList<String>();
boolean completedFirstSection = false;
int count = 2;
String addString = "";
String nextLine;
while ((nextLine=br.readLine())!=null) {
if(count < 990) {
addString += nextLine;
count++;
} else {
String sub = nextLine.substring(16);
sub = sub.substring(0, sub.length());
addString = sub + ":{" + nextLine + "," + addString.substring(0, addString.length()-2) + br.readLine();
br.readLine();
allData.add(addString);
count = 2;
}
}
allData.add("}}]");
System.out.println("here");
System.out.println(returnFile.toString());
finalString = returnFile;
br.close();
} catch (IOException e) {
e.printStackTrace();
}
collegeData 2.txt is 44.2 mb and it is a json file. Here is my error message.
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.base/jdk.internal.misc.Unsafe.allocateUninitializedArray(Unsafe.java:1250)
at java.base/java.lang.invoke.StringConcatFactory$MethodHandleInlineCopyStrategy.newArray(StringConcatFactory.java:1605)
at java.base/java.lang.invoke.DirectMethodHandle$Holder.invokeStatic(DirectMethodHandle$Holder)
at java.base/java.lang.invoke.LambdaForm$BMH/754666084.reinvoke(LambdaForm$BMH)
at java.base/java.lang.invoke.LambdaForm$MH/801197928.linkToTargetMethod(LambdaForm$MH)
at RunnerCombined.main(RunnerCombined.java:31)
RunnerCombined.java:31 is 'addString += nextLine' above.
Thank you in advance for your help!
Thank you to twain249! After changing my code to this, my program runs quickly and it's perfect!
StringBuffer finalString = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new FileReader("collegeData 2.txt"));
StringBuffer returnFile = new StringBuffer();
returnFile.append(br.readLine() + "\n" + br.readLine() + "\n");
boolean completedFirstSection = false;
int count = 2;
StringBuffer addString = new StringBuffer();
String nextLine;
while ((nextLine=br.readLine())!=null) {
if(count < 990) {
addString.append(nextLine + "\n");
count++;
} else {
String sub = nextLine.substring(16);
sub = sub.substring(0, sub.length());
// addString = sub + ":{" + nextLine + "," + addString.substring(0, addString.length()-2) + br.readLine();
returnFile.append(sub + ":{" + nextLine + "," + "\n");
returnFile.append(addString.substring(0, addString.length()-2) + "\n");
returnFile.append(br.readLine() + "\n");
addString.setLength(0);
count = 2;
br.readLine();
}
}
returnFile.append("}}]");
finalString = returnFile;
br.close();
} catch (IOException e) {
e.printStackTrace();
}
I want to change the score of the user, but instead of overwriting the file with new scores, it just makes an empty file.
The lines are written like this: "username-password-wins-losses-ties"
try {
BufferedReader br = new BufferedReader(new FileReader("users.txt"));
PrintWriter pw = new PrintWriter(new FileWriter("users.txt"));
String[] tab = null;
String zlepek = "";
while(br.readLine()!=null) {
String line = br.readLine();
tab = line.split("-");
int countLoss = Integer.parseInt(tab[3]+plusLoss);
int countWin = Integer.parseInt(tab[2]+plusWin);
int countTie = Integer.parseInt(tab[4]+plusTie);
if(tab[0].equals(loginUser.user)) {
String newScore = (tab[0] + "-" + tab[1] + "-" + countWin + "-" + countLoss + "-" + countTie + "\n");
zlepek = zlepek+newScore;
} else {
String oldScore = (tab[0] + "-" + tab[1] + "-" + tab[2] + "-" + tab[3] + "-" + tab[4] + "\n");
zlepek = zlepek+oldScore;
}
}
pw.print(zlepek);
pw.close();
br.close();
plusWin = 0;
plusLoss = 0;
plusTie = 0;
} catch(IOException e) {}
Any help is appreciated.
You are skipping the first line and every odd lines, change the code like below
String line = null;
while ((line = br.readLine()) != null){
// code
}
also you are reading and writing on a same file, you can't read and write simultaneously on a same file. If you want you can write it to a temporary file and rename it later.
If you want write on the same file, you have to close the reader before writing, see below
BufferedReader br = new BufferedReader(new FileReader("users.txt"));
String line = null;
StringBuilder sb = new StringBuilder();
String zlepek = "";
while ((line = br.readLine()) != null) {
zlepek = // logic to get value
sb.append(zlepek).append("\n");
}
br.close();
PrintWriter pw = new PrintWriter(new FileWriter("users.txt")); // pass true to append
pw.print(sb);
pw.close();
The problem is with this line while(br.readLine()!=null), you read the line but didnt save it, so it doesnt know where it is and the line will always be empty after you read the line and dont save it.
try to remove this String line = br.readLine(); and read it in the while. define the String line; and while( (line = br.readLine()) != null){ // do something}
try {
String[] tab = null;
String line = null;
tab = line.split("-");
int countLoss = Integer.parseInt(tab[3]+plusLoss);
int countWin = Integer.parseInt(tab[2]+plusWin);
int countTie = Integer.parseInt(tab[4]+plusTie);
BufferedReader br = new BufferedReader(new FileReader("users.txt"));
StringBuilder sb = new StringBuilder();
String zlepek = "";
while ((line = br.readLine()) != null) {
if(tab[0].equals(loginUser.user)) {
String newScore = (tab[0] + "-" + tab[1] + "-" + countWin + "-" + countLoss + "-" + countTie + "\n");
zlepek = zlepek+newScore;
} else {
String oldScore = (tab[0] + "-" + tab[1] + "-" + tab[2] + "-" + tab[3] + "-" + tab[4] + "\n");
zlepek = zlepek+oldScore;
}
sb.append(zlepek).append("\n");
}
br.close();
PrintWriter pw = new PrintWriter(new FileWriter("users.txt")); // pass true to append
pw.print(sb);
pw.close();
plusWin = 0;
plusLoss = 0;
plusTie = 0;
} catch(IOException e) {}
}
So Im working of reading a file containing appointments that I wrote to earlier in my code. I want to sift through the text file and find appointments on a certain date and add them to an ArrayList but when the BufferedReader goes through it, it skips ever other line... Heres my code
public ArrayList<String> read(int checkDay, int checkMonth, int checkYear) {
ArrayList<String> events = new ArrayList<String>();
BufferedReader in = null;
String read;
try {
in = new BufferedReader(new FileReader("calendar.txt"));
while ((read = in.readLine()) != null) {
read = in.readLine();
String[] split = read.split(",");
System.out.println(read);
if (split[1].equals(Integer.toString(checkDay)) && split[2].equals(Integer.toString(checkMonth)) && split[3].equals(Integer.toString(checkYear))) {
events.add(split[0] + " : " + split[1] + "/" + split[2] + "/" + split[3]);
}
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) {
}
}
return events;
}
You are reading the line twice..
while ((read = in.readLine()) != null) { // here
read = in.readLine(); // and here
You have error here:
while ((read = in.readLine()) != null)
read = in.readLine();
you should keep the read = in.readLine() in the while. and remove the other line.
pl try this
you r using "read = in.readLine())" two times in while loop that why it is skiping the lomes
public ArrayList<String> read(int checkDay, int checkMonth, int checkYear) {
ArrayList<String> events = new ArrayList<String>();
BufferedReader in = null;
String read;
try {
in = new BufferedReader(new FileReader("calendar.txt"));
while ((read = in.readLine()) != null) {
String[] split = read.split(",");
System.out.println(read);
if (split[0].equals(Integer.toString(checkDay)) && split[1].equals(Integer.toString(checkMonth)) && split[2].equals(Integer.toString(checkYear))) {
events.add(split[0] + " : " + split[1] + "/" + split[2] + "/" + split[3]);
}
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) {
}
}
return events;
public String runMsg(String fileName, File Path, int option)
{
int x = 0;
String name = " ", ret = "";
if (option == 1) {
x = fileName.indexOf(".c");
name = fileName.substring(0, x);
command = name + ".exe < C:\\iptest\\input.txt > C:\\outtest\\" + name + ".txt";
} else if (option == 2) {
x = fileName.indexOf(".cpp");
name = fileName.substring(0, x);
command = name + ".exe < C:\\iptest\\input.txt > C:\\outtest\\" + name + ".txt";
} else {
x = fileName.indexOf(".java");
name = fileName.substring(0, x);
command = "java " + name + " < C:\\iptest\\input.txt > C:\\outtest\\" + name + ".txt";
}
String output = executeCommand(command, Path);
}
private String executeCommand(String command, File Path)
{
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command, null, Path);
p.waitFor();
BufferedReader reader1 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
BufferedReader reader2 = new BufferedReader(new InputStreamReader(p.getInputStream()));
// BufferedReader reader3 = new BufferedReader(new InputStreamReader(p.getOutputStream()));
String line = "";
while ((line = reader1.readLine()) != null) {
output.append(line + "\n");
}
while ((line = reader2.readLine()) != null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
I am trying to execute a .class file from another java file. The .java file will take input from a txt file located at C:\iptest\input.txt and will produce an output file at the following location C:\outputtest\out.txt but when I run this application nothing happens and the application goes into some kind of infinite loop.
If I have 2 files say ABCD.txt and DEF.txt. I need to check if the String "ABCD" is present in DEF.txt and also the string "DEF" present in ABCD.txt and write the combination to a file.
Totally I have around 15000 files and each file contain nearly 50 - 3000 lines has to be searched. I wrote a piece of code, its working.. but it takes one hour to display the entire list...
Is any better way of performing this? Please suggest me.
public void findCyclicDependency(){
Hashtable<String, String> htFileNameList_1 = new Hashtable<String, String>();
Hashtable<String, String> htCyclicNameList = new Hashtable<String, String>();
FileWriter fwCyclicDepen = null;
PrintWriter outFile = null;
FileInputStream fstream = null;
FileInputStream fstream_1 = null;
DataInputStream in = null;
BufferedReader br = null;
DataInputStream in_1 = null;
BufferedReader br_1 = null;
String strSV_File_CK="";
boolean bFound = false;
File fileToSearch = null;
String strSVFileNameForComparison = "";
String strSVDependencyFileLine = "";
String strSVDFileLineExisting = "";
String strCyclicDependencyOut = "";
try {
File baseInputDirectory = new File(strInputPath);
List<File> baseInputDirListing = FileListing.getFileListing(baseInputDirectory);
// Printing out the filenames for the SodaSystem
for (File swPackage : baseInputDirListing)
{
if (swPackage.isDirectory() && swPackage.getName().endsWith("Plus")) {
List<File> currSwPackageFileListing = FileListing.getFileListing(swPackage);
System.out.println("\n swPackage File --> " + swPackage.getName() );
strCyclicDependencyOut = strOutputPath + "_"+ swPackage.getName() + "_CyclicDependency.xml";
System.out.println("\n strCyclicDependencyOut File --> " + strCyclicDependencyOut );
fwCyclicDepen = new FileWriter(strCyclicDependencyOut);
outFile = new PrintWriter(new BufferedWriter(fwCyclicDepen));
outFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
outFile.write("<CyclicDependencyFile>");
for (File DependentFile : currSwPackageFileListing) {
strSV_File_CK = DependentFile.getName().substring(0, (DependentFile.getName().length() - 4)).trim();
htFileNameList_1.put(strSV_File_CK.toUpperCase(),strSV_File_CK.toUpperCase());
}
for (File DependentFile : currSwPackageFileListing)
{
fstream = new FileInputStream(DependentFile);
// Get the object of DataInputStream
in = new DataInputStream(fstream);
br = new BufferedReader(new InputStreamReader(in));
strSVFileNameForComparison = DependentFile.getName().substring(0, (DependentFile.getName().length() - 4)).trim();
//Read File Line By Line
while ((strSVDependencyFileLine = br.readLine()) != null)
{
bFound = false;
if (strSVDependencyFileLine.toUpperCase().indexOf("INDICES") == -1)
{
//Check the current line matches any of the file name in software package folder
if (htFileNameList_1.contains(strSVDependencyFileLine.trim().toUpperCase())
&& strSVDependencyFileLine.compareTo(strSVFileNameForComparison) != 0)
{
bFound = true;
// Get the file to search
for (File searchFile : currSwPackageFileListing)
{
if((searchFile.getName().substring(0, (searchFile.getName().length() - 4)).trim()).equals(strSVDependencyFileLine))
{
fileToSearch = searchFile;
break;
}
}
// Read the file where the file name is found
fstream_1 = new FileInputStream(fileToSearch);
in_1 = new DataInputStream(fstream_1);
br_1 = new BufferedReader(new InputStreamReader(in_1));
while ((strSVDFileLineExisting = br_1.readLine()) != null)
{
if (strSVDFileLineExisting.toUpperCase().indexOf("EXTRA") == -1)
{
if (htFileNameList_1.contains(strSVDFileLineExisting.trim().toUpperCase()) && bFound
&& strSVDFileLineExisting.compareTo(strSVDependencyFileLine) != 0
&& strSVDFileLineExisting.compareTo(strSVFileNameForComparison) == 0 )
{
if(!htCyclicNameList.containsKey(strSVDependencyFileLine) &&
!htCyclicNameList.containsValue(strSVDFileLineExisting))
{
htCyclicNameList.put(strSVDFileLineExisting,strSVDependencyFileLine);
outFile.write("<CyclicDepedency FileName = \"" + strSVDFileLineExisting + "\""+ " CyclicFileName = \"" +
strSVDependencyFileLine + "\" />");
break;
}
}
}
}
}
}
else
{
bFound = false;
}
}//if current line <>
}// reach each line in the current file
outFile.write("</CyclicDependencyFile>");
}
outFile.flush();
outFile.close();
}
}
catch(Exception e){
e.printStackTrace();
}
}
Thanks
Ramm
There are several problems with your design. The most important one is repeated scanning of the file system. Try the code below.
static public void findCyclicDependency2() {
PrintWriter outFile = null;
Map<String,File> fileNames = new HashMap<String,File>();
Map<String,Set<String>> fileBackward = new HashMap<String,Set<String>>();
Map<String,Set<String>> fileForward = new HashMap<String,Set<String>>();
try {
File baseInputDirectory = new File(strInputPath);
List<File> baseInputDirListing = getFileListing(baseInputDirectory);
// Printing out the filenames for the SodaSystem
for(File swPackage:baseInputDirListing) {
if (! (swPackage.isDirectory()
|| swPackage.getName().endsWith("Plus"))) continue;
System.out.println("Loading file names");
List<File> currSwPackageFileListing = getFileListing(swPackage);
for(File dependentFile:currSwPackageFileListing) {
String name = trimName(dependentFile);
fileNames.put(name,dependentFile);
BufferedReader br = new BufferedReader(new FileReader(dependentFile));
String line;
Set<String> contFor = new HashSet<String>();
Set<String> contBack = new HashSet<String>();
while( (line=br.readLine()) != null ) {
line = line.toUpperCase().trim();
if( line.equals("EXTRA") ) continue;
if( line.equals("INDICES") ) continue;
if( line.equals(name) ) continue;
if( line.compareTo(name) == 1 ) {
contFor.add(line);
} else {
contBack.add(line);
}
}
fileBackward.put(name,contBack);
fileForward.put(name,contFor);
}
String strCyclicDependencyOut = strOutputPath + "_"
+ swPackage.getName() + "_CyclicDependency.xml";
outFile = new PrintWriter(new BufferedWriter(new FileWriter(strCyclicDependencyOut)));
outFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
outFile.write("<CyclicDependencyFile>");
for(Entry<String,Set<String>> entry : fileForward.entrySet()) {
String curr = entry.getKey();
for(String other : entry.getValue()) {
Set<String> otherRefs = fileBackward.get(other);
if( otherRefs == null ) continue;
if( otherRefs.contains(curr) ) {
outFile.write("<CyclicDepedency FileName = \""
+ fileNames.get(curr).getPath()
+ "\""
+ " CyclicFileName = \""
+ fileNames.get(other).getPath()
+ "\" />");
}
}
}
outFile.write("</CyclicDependencyFile>");
outFile.flush();
outFile.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Lucene comes to my mind.
Maybe it's more efficient to index all files, then query for the file names and use the results to detect your circular dependencies.