File.delete() isn't deleting the .Json file in Java - java

I am trying to delete a json file I just created, The file is in the same directory as the workplace so I didn't include it when creating the new File object.
I also closed the output stream before trying to delete the file by using .close() method.
import java.io.File;
public static void processFilesForValidation(String name, Scanner obj, PrintWriter logFile) {
int count = 0;
PrintWriter outputStream = null;
String[] line = obj.nextLine().split(",", -1);
if (count == 0) {
for (int i = 0; i < line.length; i++) {
try {
if (line[i].equals(" ")) {
throw new CSVFileInvalidException();
} else {
try {
outputStream = new PrintWriter((new FileOutputStream(name + ".json", true)));
} catch (FileNotFoundException e) {
System.out.println("Could not create json file " + name
+ ". Program will terminate after deleting any corrupted files and closing any opened files.");
obj.close();
outputStream.close();
String fname = name + ".json";
File file = new File(fname);
file.delete();
System.exit(0);
}
}
} catch (CSVFileInvalidException eCsvFileInvalidException) {
System.out.println(
"File " + name + ".CSV is invlaid: field is missing. \nFile is not converted to JSON.");
int detected = line.length - 1;
logFile.println("\nFile " + name + ".CSV is invalid. \nMissing field:" + detected
+ " detected, 1 missing");
for (int j = 0; j < line.length; j++) {
if (line[j].equals(" ")) {
logFile.print(line[j] + "***, ");
} else {
logFile.print(line[j] + ", ");
}
}
logFile.close();
outputStream.close();
String fname= name+".JSON";
File file = new File(fname);
file.delete();
break;
}
}
}
}
The code is a little long but this is the method, I hope you can help me.

You open a new outputStream every time around the loop. As far as I can tell, you only close it on a couple of errors. So, on loop #2, the first outputStream is left dangling and not closed.

Related

Writer dont writing in file but only create it

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.

I need to read a .txt file from a java and write the sorted context to a different .txt file

I have a I/O java file, a SDive file which contains main, a .txt Directory file that has random words in it and a Sorted .txt to return the random words in order ascending to descending. It print all the words in the Sorted but it is not sorted.
//Sort.java
// required for input
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
// required for output
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
class Sort{
private String[] tArr = new String[100];
public void swap(int j) {
String temp = tArr[j-1];
tArr[j-1] = tArr[j];
tArr[j] = temp;
}
///Bubble sort
public void sort() {
int n = tArr.length;
for(int i = 0; i < n; i++) {
for(int j = 1; j < n-i; j++) {
if(tArr[j-1].compareTo(tArr[j]) > 0) {
swap(j);
}
}
}
}
public void read() {
System.out.println("in read()");
String pwd = System.getProperty("user.dir");
String fileName = pwd + "/directory.txt";
System.out.println("Looking for: " + fileName);
String fileLine = "";
try {
File f = new File(fileName);
if(f.exists()) {
System.out.println("Directory profile found, loading data...");
}
else {
System.out.println("Directory profile not found, loading default...");
return; // done, return back to the caller
}
// Read file
FileReader data = new FileReader(fileName);
// Wrap FileReader with BufferedReader
BufferedReader br = new BufferedReader(data);
//String tmp;
int i=0;
while ((fileLine = br.readLine()) != null) {
tArr[i++] = fileLine;
}
// ok, time to load an existing profile
// close the file
br.close();
} catch (FileNotFoundException fnf) {
System.out.println("File not found: " + fileName);
} catch (IOException ioe) {
System.out.println("Error reading file: " + fileName);
} catch (Exception exc) {
System.out.println("Something went wrong: " + fileName);
}
}
public void write() {
System.out.println("in write()");
String pwd = System.getProperty("user.dir");
String fileName = pwd + "/Sorted.txt";
try {
System.out.println("Writing out to: " + fileName);
File file = new File(fileName);
// creates the file
file.createNewFile();
// create FileWriter object
FileWriter writer = new FileWriter(file);
// output to file
// ADD pIdx, pArr, mood, and anything else here...
for(int i = 0; i < tArr.length; i++) {
writer.write(tArr[i] + "\n");
}
writer.flush();
writer.close();
} catch (IOException ex) {
System.out.println("Error writing to file: " + fileName);
}
}
}
And my main file is SDrive:
class SDriver{
public static void main(String args []){
Sort io = new Sort();
io.read();
io.write();
}
}
You should add the io.sort() method after io.read(); line and just before io.write(); line.
I see you use bubble sort, if you really want to implement your own sorting method take a look at quick sort and merge sort which are much much faster than bubble sort on larger arrays but also harder to implement. Insertion and Selection sort are not as fast as merge or quick but still faster than bubble and still as easy to self implement. Or use Arrays.sort(tArr); if you want to do it quick.

StackOverFlowError BufferedWriter issue

I'm programming a conversion tool that should take a json file as an input. Due to the problem that json input files may be very large I'll perform a split at the beginning. I have to resort the structure of the input file and because I don't want to keep the whole large file in my memory all the time, I'll split it.
I'm proofing the occurences of a json block by counting the { and }. If the counter is 0, a split should be performed. That's where the problem is: if the file is about 40MBs large the JVM throws a StackOverFlowError and I don't know why.
Here's what I do to perform a split:
/*
* Utility-function to read a json file part by part and save the parts to a separate json file.
* #param scanner The scanner which contains the file and which returns the lines from the file.
* #param j The counter of the file. As the file should change whenever the counter changes.
* #return jsonString The content of the jsonString.
*/
public String readPartOfFileAndSave(String filepath, Scanner scanner, int j) {
String jsonString = "";
int i = 0;
++j;
while (scanner.hasNext()) {
String token = scanner.next();
jsonString += token + System.lineSeparator();
if (token.contains("{")) {
i++;
}
if (token.contains("}")) {
i--;
}
if (i == 0) {
// DEBUG
// if (j % 1000 == 0) {
// System.gc();
// System.out.println("Garbage Collector called manually");
// }
saveFile(filepath, "actor", j, jsonString);
readPartOfFileAndSave(filepath, scanner, j);
}
}
return "";
}
/*
* #param filename The name of the target file where the content is saved to.
* #param j The counter of the file. As the file should change whenever the counter changes.
* #param content The content of the file.
*/
public void saveFile(String filepath, String fileprefix, int j, String content) {
File file = null;
if (this.osValidator.isWindows()) {
file = new File(filepath + "\\" + fileprefix + "" + j + ".json");
} else {
file = new File(filepath + "/" + fileprefix + "" + j + ".json");
}
try {
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file.getAbsoluteFile()), "UTF-8"));
bw.write(content);
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The exception looks like this:
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at java.util.Hashtable.get(Hashtable.java:363)
at java.util.Properties.getProperty(Properties.java:969)
at java.lang.System.getProperty(System.java:717)
at sun.security.action.GetPropertyAction.run(GetPropertyAction.java:84)
at sun.security.action.GetPropertyAction.run(GetPropertyAction.java:49)
at java.security.AccessController.doPrivileged(Native Method)
at java.io.BufferedWriter.<init>(BufferedWriter.java:109)
at java.io.BufferedWriter.<init>(BufferedWriter.java:88)
at utility.ReadFileAndSave.saveFile(ReadFileAndSave.java:183)
at utility.ReadFileAndSave.readPartOfFileAndSave(ReadFileAndSave.java:158)
at utility.ReadFileAndSave.readPartOfFileAndSave(ReadFileAndSave.java:159)
splitFile() is called in a separate class. But nothing else happens there.
public void splitFile() {
try {
ReadFileAndSave reader = new ReadFileAndSave();
String jsonFilePath = this.converterView.sourceTextField.getText();
File jsonFile = new File(jsonFilePath);
Scanner scanner = new Scanner(new FileInputStream(jsonFile), "UTF-8");
int j = 0;
File newDir = null;
if (this.osValidator.isWindows()) {
newDir = new File(System.getProperty("user.home") + "\\temp\\");
} else {
newDir = new File(System.getProperty("user.home") + "/temp/");
}
if (!newDir.exists()) {
newDir.mkdir();
}
if (this.osValidator.isWindows()) {
reader.readPartOfFileAndSave(System.getProperty("user.home") + "\\temp\\", scanner, j);
} else {
reader.readPartOfFileAndSave(System.getProperty("user.home") + "/temp/", scanner, j);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(FilterController.class.getName()).log(Level.SEVERE, null, ex);
}
}
The input file has about 38.000 blocks in it. About 5.200 will be splitted to separate files. Then the exception is thrown.
The JVM seems to have a problem with the BufferedWriter. Does anyone know how to fix this issue?

how to output file lists to text file using java

hello i am a newbie to java. i just started learning java last week.
below is a code I am using to display all files and the corresponding file sizes of a folder and it's subfolder.
However, instead of displaying the output in the Eclipse console, what I need to achieve is actually output the same data to a text file. I've been searching on the net on how to accomplish this over the last couple of days but I wasn't able to come to a solution.
Can someone advise me on what code to use to accomplish my task?
Thanks so much!
public class ReadFile1 {
public static void main(String[] a)throws IOException{
showDir(1, new File("/Users/User/Documents/1 eclipse test/testfolder1"));
//File file = new File("/Users/User/Documents/1 eclipse test/testfolder1/puppy4.txt");
//long fileSize = file.length();
}
static void showDir(int indent, File file) throws IOException {
for (int i = 0; i < indent; i++)
System.out.print('-');
System.out.println(file.getName() + " - " + file.length() / 1024 + " KB");
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++)
showDir(indent + 4, files[i]);
}
}
}
Here is your example converted :
public class ReadFile1
{
public static void main(String[] a) throws IOException
{
FileWriter fstream = new FileWriter("C:\\test.txt",true);
BufferedWriter out = new BufferedWriter(fstream);
showDir(out,1,new File("C:\\"));
out.flush();
out.close();
}
static void showDir(BufferedWriter writer, int indent, File file) throws IOException
{
for(int i = 0; i < indent; i++)
{
writer.write('-');
//System.out.print('-');
}
writer.write(file.getName() + " - " + file.length() / 1024 + " KB");
writer.newLine();
//System.out.println(file.getName() + " - " + file.length() / 1024 + " KB");
if(file.isDirectory())
{
File[] files = file.listFiles();
for(int i = 0; i < files.length; i++)
{
showDir(writer,indent + 4, files[i]);
}
}
}
}
Update your showDir with the file writing code as mentioned here:
File file = new File("info.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
PrintWriter writer = new PrintWriter(file);
writer.println(file.getName() + " - " + file.length() / 1024 + " KB");
writer.close();
This will put the console output into the text file:
try{
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
}catch(SecurityException se){
//Exception handling
}
setOut(PrintStream out) reassigns the "standard" output stream.It throws SecurityException -- if a security manager exists and its checkPermission method doesn't allow reassigning of the standard output stream.

Java Having trouble saving file

I am trying to have my program save, but whenever I try to save a program that exists (so the second run through) it creates the temp, but it does not overwrite the old file.
Here is the code. Can anyone find out why it does not overwrite the old file with the new?
public static void saveBallotData(int i)throws IOException{
PrintWriter outputFile;
outputFile = new PrintWriter("temp2.txt");
File tempCheck = new File (list.get(i).getBallotNumber()+".txt");
if(tempCheck.exists()){
Scanner inputFile = new Scanner(tempCheck);
for(int m = 0; m < list.get(i).getNumberOfChoices(); m++){
if(list.get(i).getVote().equals(list.get(i).getChoice(m))){
//outputFile.println(list.get(i).getChoice(m) + ":" + getInt(m, tempCheck) + 1);
inputFile.nextLine();
}
else{
outputFile.println(inputFile.nextLine());
}
}
}
else{
for(int a = 0; a < list.get(i).getNumberOfChoices(); a++){
if(list.get(i).getVote().equals(list.get(i).getChoice(a))){
outputFile.println(list.get(i).getChoice(a) + ":" + "1");
}
else{
outputFile.println(list.get(i).getChoice(a) + ":" + "0");
}
}
}
System.out.println("PHE");
outputFile.close();
File g = new File("temp2.txt");
File f = tempCheck;
f.delete();
g.renameTo(f);
}

Categories

Resources