Delete method doesn't delete - Java - java

I made a program that can display and edit a record. The problem is that I cannot delete the file that I wanted to delete to replace it with the edited ones.
public class Laboratory {
public static void main(String[] args) throws FileNotFoundException,InterruptedException,IOException {
// paste your script here ;)
String fileName = "record.txt";
String filepath = "D:\\Programming\\Java\\Program - Script Test\\files\\" + fileName;
String in = "";
File file = new File(filepath);
Scanner fscan = new Scanner(file);
Scanner scan = new Scanner(System.in);
PrintWriter pw = null;
int linecount = 1;
String content = "";
// reads the file according to the given line count.
for(int i = 0; i < linecount; i++) {
content = fscan.nextLine();
}
// creates the template file.
String tempPath = "D:\\Programming\\Java\\Program - Script Test\\files\\" + "temp.txt";
String contentParts[] = content.split("\\|");
System.out.println(content);
System.out.println(contentParts[1]);
System.out.println(contentParts[2]);
System.out.print("change the name >> ");
in = scan.nextLine();
// edits the scanned content from the file.
String finalContent = "|" + in + "|" + contentParts[2];
System.out.println(finalContent);
file = new File(filepath);
fscan = new Scanner(file);
// scans the original record and pastes it in a new template file.
try {
pw = new PrintWriter(new FileOutputStream(tempPath));
if(linecount == 1) {
content = fscan.nextLine();
pw.println(finalContent);
while(fscan.hasNextLine()) {
content = fscan.nextLine();
pw.println(content);
}
}
else if (linecount > 1) {
for (int i = 0; i < linecount - 1; i++) {
content = fscan.nextLine();
pw.println(content);
}
pw.println(finalContent);
content = fscan.nextLine();
while (fscan.hasNextLine()) {
content = fscan.nextLine();
pw.println(content);
}
}
}
catch(FileNotFoundException e) {
System.out.println(e);
}
finally {
pw.close();
fscan.close();
}
// deletes the original record
file.delete();
} // end of method
} // script test class end
Although, I made a test program that successfully deletes a file.
public class delete {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
File file = new File("helloworld.txt");
String in;
System.out.println("Press ENTER to DELETE file.");
in = scan.nextLine();
file.delete();
} // main method end
} // program end
My file path is right and I don't really know what causes the problem. Is there a solution to fix this?

Explanation
file.delete() does not throw an error if it failed. And it failed here, as indicated by its return value being false.
Execute Files.delete(file.toPath()) instead and you will see the exact error reason, which is:
Exception in thread "main" java.nio.file.FileSystemException: D:\Programming\Java\Program - Script Test\files\record.txt: The process cannot access the file because it is being used by another process
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:92)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:274)
at java.base/sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:105)
at java.base/java.nio.file.Files.delete(Files.java:1146)
at Laboratory.main(Laboratory.java:123)
So
The process cannot access the file because it is being used by another process
Because you still have a scanner to the file open, you are blocking yourself from deleting it. Close the scanner and it will work.
Your code opens two (not one) scanner to file, one at the beginning:
Scanner fscan = new Scanner(file);
which you use during your initial loop:
for (int i = 0; i < linecount; i++) {
content = fscan.nextLine();
}
and then later on you create a second one:
fscan = new Scanner(file);
which you also close during your finally block:
fscan.close();
But you did never close the first scanner.
Solution
Add
fscan.close();
After the initial loop:
for(int i = 0; i < linecount; i++) {
content = fscan.nextLine();
}
fscan.close();
and the file.delete() will succeed.
NIO
As explained, file.delete() is a poorly designed method. Prefer Files.delete(path).
In general, if you do not have a good reason to use the old cumbersome file IO library, dont. Use NIO instead (Java 7+).

Related

Return array initialized inside try/catch

I am creating a program which creates reads a file into an array separating which file into a different index value in the array.
static String[] readFile () {
int count = 0;
try {
File file = new File("input.txt"); // create file
Scanner scanner = new Scanner(file); // create scanner associated to file
// counts number of lines
while (scanner.hasNextLine()) {
scanner.nextLine();
count++;
}
// reads file into array
while (scanner.hasNextLine()) {
String[] data = new String[count];
int len = data.length;
for (int i = 0; i <= len; i++) {
data[i] = scanner.nextLine();
}
}
} catch (Exception e) {
System.out.println("File not found!!!");
System.exit(0);
}
return data;
}
The problem is that when trying to return the variable data I get an error saying 'cannot resolve symbol data" because it is initialized in a try-catch block. I have tried doing this but it returns the value null because the variable's length is determined by the variable count whose's value is also determined in a catch block. Thanks in advance!
You can use #Sweeper advice from comments. It will be looks like this.
static ArrayList<String> readFile () {
ArrayList<String> data = new ArrayList<>();
try {
File file = new File("input.txt"); // create file
Scanner scanner = new Scanner(file); // create scanner associated to file
while (scanner.hasNextLine()) {
data.add(scanner.nextLine()) ;
}
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
But if you want to stay with your current code, you can initialize data by null out of try block. And also you need to reset Scanner. Your code will be looking something like this. Note, that in the for loop you must use condition <len not <=len.
static String[] readFile () {
String[] data = null;
try {
File file = new File("input.txt"); // create file
Scanner scanner = new Scanner(file); // create scanner associated to file
// counts number of lines
int count = 0;
while (scanner.hasNextLine()) {
scanner.nextLine();
count++;
}
scanner.close(); // don't forget about closing resources
data = new String[count];
// reads file into array
scanner = new Scanner(file);
for (int i = 0; i < len; i++) {
data[i] = scanner.nextLine();
}
scanner.close();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
Here are some similar questions with answers:
Java: Reading a file into an array
Read text file into an array
Also, I want to point at the try-with-resources statement - the Scanner object should be closed or initialized inside it.
Additionally, System.exit(0); is not a good way to stop a method, because all finally blocks around it wouldn't be executed in this case.
you are having 2 problems the firsthappens because the variable data is declared in the try-catch block ¿what if an instruction throws an exeption and the variable data is never declared? in this case ¿what is going to be returned?, the solution is to declare the variable data before the try-catch block, the second happens because when you invoke nextLine() the Scanner object mantains its state so when you try to invoke nextLine() again after go through the whole file it is in the last line (there is not next line), you can solve it invoking close() and then initialize the scanner object again this will reset the state:
static String[] readFile () {
Scanner scanner = null;
int count = 0;
String[] data = null;
try {
File file = new File("C:\\Users\\Mulé\\Desktop\\doc.txt"); // create file
scanner = new Scanner(file); // create scanner associated to file
// counts number of lines
while (scanner.hasNextLine()) {
scanner.nextLine();
count++;
}
scanner.close();
scanner = new Scanner(file);
// reads file into array
data = new String[count];
for (int i = 0; i < data.length; i++) {
data[i] = scanner.nextLine();
}
} catch (Exception e) {
System.out.println("File not found!!!");
System.exit(0);
}
return data;
}

Using formulas from a text/writing to a text

So, my lecture powerpoint slides and even my book is not really doing a good job (for my understanding that is) of explaining how to use formulas from a text document, then when the code runs/compiles successfully it will create a "Results.txt" in the same folder.
These are the formulas in a notepad doc. Nothing to crazy, just a proof of concept
4 * 5 ..
3 / 4...
3 - 1..
2 + 3..
import java.io.*;
import java.util.*;
public class ReadFileLineByLine {
public static void main(String[] args) throws FileNotFoundException {
String line;
int numberOfLines = 3;
String[] textData = new String[numberOfLines];
int i;
for(i = 0; i < numberOfLines; i++){
textData[i] = textReader.readLine();
}
text.Reader.close();
return textData;
try {
File inputfile = new File(args[0]); //new File("formulas.txt")
Scanner input = new Scanner(new File("C:\Users\Frost\Documents\Question4"));
BuffredReader br = new BufferedReader(new FileReader("C:\Users\Frost\Documents\Question4"));
PrintWriter output = new PrintWriter("Results.txt");
while (input.hasNextLine()) {
line = input.nextLine();
System.out.println("read <" + line + ">"); // Display message to commandline
// Declare ArrayList of for storing tokenized formula from String line
double result = 0; // The variable to store result of the operation
// Determine the operator and calculate value of the result
System.out.println(formula.get(0) + ' ' + formula.get(1) + ' ' +
formula.get(2) + " = " + result); // Display result to command line
// Write result to file
}
// Need to close input and output files
}
catch (FileNotFoundException e) {
System.out.println("Error reading file named " + Formulas.txt);
}
}
}
Here's something to get you started. The //TODO: comments are where you need to build your logic. Be sure to change the file paths back to what you need. I changed them to a Temp location. Also change the messages printed as I just put something there as proof of concept. I tried to comment thoroughly but don't hesitate to ask questions.
import java.io.*;
import java.util.*;
public class ReadFileLineByLine {
public static void main(String[] args) throws FileNotFoundException {
String line = "";
//Declare Scanner and PrintWriter outside of try clause so they can be closed in finally clause
Scanner input = null;
PrintWriter output = null;
try {
//Instantiate input and output file
input = new Scanner(new File("C:\\Temp\\test.txt"));
output = new PrintWriter(new File("C:\\Temp\\Results.txt"));
//Loop through lines in input file
while (input.hasNextLine()) {
line = input.nextLine();
// Display message to commandline
System.out.println("read <" + line + ">");
// Populate ArrayList of tokenized formula from String line
//TODO:
// The variable to store result of the operation
double result = 0;
// Determine the operator and calculate value of the result
//TODO:
// Write result to file
output.println("Print result of " + line + " to Results.txt");
}
} catch (FileNotFoundException e) {
//Exception thrown, print message to console
System.out.println("File Not Found: " + e.getMessage());
} finally {
//close files in finally clause so it happens even if exception is thrown
//I also set to null as extra precaution
input.close();
input = null;
output.close();
output = null;
}
}
}

JAVA Help : Append file and remove last character

am I still very new to java, only had one semester of it. I have my first internship and it isn't a programming internship, just a general IT internship Since it was only my first semester.
My boss does not know Java, nor does anyone in the building. He knew I had some basic programming experience and told me to take a stab at the problem he is having. He has a report that is saved and the very last line, the very last character of the report is a character turn symbol, and we need remove that because it is giving us problems on the website.
I am not sure if I am even on the right track, at this point I am just doing trial and error. Please help :D
public class RemoveChar {
public static void main(String[] args) throws FileNotFoundException {
// Variables and stuff
Scanner keyScan = new Scanner(System.in);
JFrame frameOne = new JFrame ("File Name");
Scanner fileScan = new Scanner(System.in);
String fileName;
// Ask user for file name
System.out.print("What is the file full file name? ");
fileName = fileScan.nextLine();
// Add .txt if the user forgets to put it in the prompt
if (!fileName.contains(".txt"))
fileName += ".txt";
//Test to see if file exists
File myFile = new File(fileName);
if(!myFile.exists()){
System.out.println(fileName + " does not exist. ");
System.exit(0);
}
fWriter = new FileWriter("config/lastWindow.txt", true);
/*while(fileName.hasNext()){
}
File
BufferedReader inputFile = new BufferedReader(new FileReader("C:\\" + fileScan));
//Scanner reader = new Scanner (inputFile);
*/
}
}
How big are the files? If they are not that large, you can read the entire file into a string and then chop off the last character:
//Set delimiter to end-of-string anchor \Z so that you can read the
//file in with just one call to next()
//from: http://stackoverflow.com/a/3403112/263004
String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();
String withoutLastCharacter = content.substring(0, content.length - 1);
Then you just need to write withoutLastCharacter out to the file.
Otherwise you need to read in the original file line by line and write it out to a temporary file, and then copy that file over the original one. However, if you are on the last line, you will chop off the last character. Here's some code that should give you an idea of the basic logic:
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
//If this is the last line chop off the last character.
if(!scanner.hasNextLine()) {
line = line.substring(0, line.length - 1);
}
//Write line out to temporary file
...
}
You also mentioned that it doesn't have to be Java. If you're on Linux or Mac, you can just do this with sed:
sed -i '$s/.$//' <filename>
This will delete the last character of the last line of the file.
Does this have to be a java problem? For something like this where it is basic file/string manipulation I prefer to use something like Perl. The below perl script will delete the last byte (or char in this case) from a file
my $fsize = -s $filename;
# print $size."\n";
open($FILE, "+<", $filename) or die $!;
seek $FILE, $size-2, SEEK_SET;
print $FILE ";";
close $FILE;
static class CopyFileContent {
public static void main(String[] args) {
String root = Environment.getExternalStorageDirectory().toString(); //get access to directory path
File myDir = new File(root + "/MyFolder");//create folder in internal storage
myDir.mkdirs();// make directory
File destFile = new File(myDir, FILENAME11);//making a new file in the folder
/* Source file, from which content will be copied */
File sourceFile1 = new File(myDir, FILENAME12);
File sourceFile2 = new File(myDir, FILENAME13);
File sourceFile3 = new File(myDir, FILENAME14);
/* destination file, where the content to be pasted */
// File destFile = new File(FILENAME);
/* if file not exist then create one */
if (!destFile.exists()) {
try {
destFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
InputStream input1 = null;
InputStream input2 = null;
InputStream input3 = null;
OutputStream output = null;
InputStream input4 = null;
try {
/* FileInputStream to read streams */
input1 = new FileInputStream(sourceFile1);
input2 = new FileInputStream(sourceFile2);
input3 = new FileInputStream(sourceFile3);
/* FileOutputStream to write streams */
output = new FileOutputStream(destFile, true);
byte[] buf = new byte[1024];
int bytesRead;
output.write("{Record:[{".getBytes());
while ((bytesRead = input1.read(buf)) > 0) {
output.write(buf, 1, bytesRead);
RandomAccessFile f = new RandomAccessFile(destFile, "rw");
long length = f.length() - 2;
f.setLength(length);
length = f.length();
f.close();
output.write(",".getBytes());
}
while ((bytesRead = input2.read(buf)) > 0) {
output.write(buf, 1, bytesRead);
RandomAccessFile f = new RandomAccessFile(destFile, "rw");
long length = f.length() - 2;
f.setLength(length);
length = f.length();
f.close();
output.write(",".getBytes());
}
while ((bytesRead = input3.read(buf)) > 0) {
output.write(buf, 1, bytesRead);
RandomAccessFile f = new RandomAccessFile(destFile, "rw");
long length = f.length() - 2;
f.setLength(length);
length = f.length();
f.close();
output.write(",".getBytes());
output.write(b.getBytes());
output.write(d.getBytes());
output.write("}]}".getBytes());
output.write("\r\n".getBytes());
}
RandomAccessFile f1=new RandomAccessFile(destFile,"rw");
long length1= f1.length()-1;
f1.setLength(length1);
f1.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != input1) {
input1.close();
}
if (null != input2) {
input2.close();
}
if (null != input3) {
input3.close();
}
if (null != output) {
output.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

producing some text file using a for loop in java?

I am going to produce some text files(number of files is entered by user) using a for loop, but when the program runs only one file is produced because the file name doesn't change. I don't know how to change the names of file every time. my code is below which produces only one text file not as many as the user enters.
Scanner sc = new Scanner(System.in);
int p1 = sc.nextInt();
for (int i = 0; i < p1; i++) {
try {
FileWriter f = new FileWriter("textfile.txt");
f.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try to give like this in for loop to create multiple file
FileWriter f = new FileWriter("textfile_"+i+".txt");
Instead of having a constant name for the file, use a String variable. If you want that the name of the file is unique each time you run your program, you can write something like this:
Scanner sc = new Scanner(System.in); int p1 = sc.nextInt();
for (int i = 0; i < p1; i++) {
try {
String filename = "textfile_" + System.currentTimeMillis() + ".txt"
FileWriter f = new FileWriter(filename);
f.close();
} catch (IOException e) {
e.printStackTrace();
}}
System.currentTimeMillis() returns the number of miliseconds from 1.1.1970 UTC

overwriting a file in java using FileWriter

I have to accomplish a task of writing a set of data to file, use it, then overwrite it with new data. Thus overwrite of the file takes place repeatedly.I know i can accomplish the above by creating FileWriter object each time with the option to overwrite like below
FileWriter object = new FileWriter("fileName", false)
and close it to write to the file.
If i am supposed to overwrite the file n number of times , according to the above method i need to create n number of FileWriter objects. Is there any efficient way to overwrite a file repeatedly by only creating a single FileWriter object?
Not a direct answer, but anyway.
DON'T DO THAT!
What do you think will happen if for some reason writing the new data to the file fails?
You not only lose your original file, but also the new file contents...
Write the new content to another file, ensure that it is well written and closed, and then rename the new file atomically to the original file.
PS: and do not forget to correctly .close().
PS2: if you use Java 7, use the new Files API.
Its better to make a temp file and then rename the tempfile and delete the old like here:
public static void nachtragenTRA(File files) throws IOException{
Scanner sc=null;
File f= files;
String analyse = "";
String NTausgabe = "";
int max = 0;
int k = 0;
String updatedLine[] = new String [4];
int filenr = 1;
boolean sucess = false;
try{
sc= new Scanner(f);
}catch(FileNotFoundException x){
System.out.println("Error: File not found!");
}
while (sc.hasNextLine()){ //get next line
analyse = sc.nextLine();
max = analyse.length(); //get line lenght
StringBuffer sb = new StringBuffer(analyse); //write analyse in StringBuffer
//to change the string
if(k == 1)
{
sb.replace(Daten.NTdatapos[3],max, Daten.NTProbentypTextfield.getText());
updatedLine[0] =String.valueOf(sb);
}
else if(k == 2)
{
sb.replace(Daten.NTdatapos[4],max, Daten.NTPrueferTextfield.getText());
updatedLine[1] =String.valueOf(sb);
}
else if(k == 3)
{
sb.replace(Daten.NTdatapos[5],max, Daten.NTKundeTextfield.getText());
updatedLine[2] =String.valueOf(sb);
}
else if(k == 4)
{
sb.replace(Daten.NTdatapos[5],max, Daten.NTWerkstoffTextfield.getText());
updatedLine[3] =String.valueOf(sb);
}
if(k>3)
{
break;
}
k++;
}
sc.close();
//NTausgabe=DatenTextarea.getText()+"\n"+updatedLine[0]+"\n"+updatedLine[1];
//DatenTextarea.setText(String.valueOf(NTausgabe));
//NTausgabe=DatenTextarea.getText()+"\n"+NTKundeTextfield.getText()+"\n"+NTPrueferTextfield.getText();
//DatenTextarea.setText(String.valueOf(NTausgabe));
//create tmp file with the new data
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(String.valueOf(f)+".tmp")));
BufferedReader br = null;
FileReader reader = null;
try {
reader = new FileReader(String.valueOf(f));
br = new BufferedReader(reader);
String line;
while ((line = br.readLine()) != null)
{
//Change speciffic lines
if(filenr == 2)
{
writer.println(updatedLine[0]);
}
else if(filenr == 3)
{
writer.println(updatedLine[1]);
}
else if(filenr == 4)
{
writer.println(updatedLine[2]);
}
else if(filenr == 5)
{
writer.println(updatedLine[3]);
}
//Andere Zeilen beibehalten
else
{
writer.println(line);
}
filenr = filenr + 1;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
reader.close();
br.close();
File realName = new File(String.valueOf(f));
realName.delete(); //delete old file
writer.close();
sucess = new File(String.valueOf(f)+".tmp").renameTo(realName); //rename tmp File to the others name
if(sucess != true)
{
NTausgabe=Daten.DatenTextarea.getText()+"\n"+"Rename File failed";
Daten.DatenTextarea.setText(String.valueOf(NTausgabe));
}
else
{
NTausgabe=Daten.DatenTextarea.getText()+"\n"+"File renamed sucessfully";
Daten.DatenTextarea.setText(String.valueOf(NTausgabe));
}
}
}

Categories