Copy,Delete,Rename File IO
Create a temporary file.
Copy from the file to the temporary file but do not copy extra blanks (replace blanks with more than 2 white spaces to 1 white space).
Then delete the original file,and rename the temporary file to be the original file.
Okay, this is what I have so far.
-It copies the file to "temp.txt"
-Deletes the original file
-Renames "temp.txt" to original file name
When I rename the file at the end, I don't know how to rename it to the original file name without giving it a specific name, instead of a global variable which I would want to be "editFile" that I used in main.
And I need it to use this code to convert extra white spaces to 1 white space, but I don't know where to put it or how to use it.
Code:
String line;
while (inputStream.hasNextLine()) {
line = inputStream.nextLine();
StringTokenizer st = new StringTokenizer(line, " ");
while (st.hasMoreTokens()) {
word = st.nextToken();
if (st.hasMoreTokens())
outputStream.print(word + " ");
else
outputStream.println(word);
}
}
Okay, this is what I have so far.
Code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class CopyFiles {
public static void main(String[] args) {
try {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter name of file to be editted.");
String editFile = keyboard.next();
copy(editFile, "temp.txt");
} catch (IOException e) {
System.err.println(e.getMessage());
}
Delete();
File file = new File("temp.txt");
File file2 = new File("fromFile.txt");
boolean success = file.renameTo(file2);
}
public static void copy(String fromFileName, String toFileName)
throws IOException {
File fromFile = new File(fromFileName);
File toFile = new File(toFileName);
if (!fromFile.exists())
throw new IOException("FileCopy: " + "no such source file: "
+ fromFileName);
if (!fromFile.isFile())
throw new IOException("FileCopy: " + "can't copy directory: "
+ fromFileName);
if (!fromFile.canRead())
throw new IOException("FileCopy: " + "source file is unreadable: "
+ fromFileName);
if (toFile.isDirectory())
toFile = new File(toFile, fromFile.getName());
if (toFile.exists()) {
if (!toFile.canWrite())
throw new IOException("FileCopy: "
+ "destination file is unwriteable: " + toFileName);
System.out.print("Overwrite existing file " + toFile.getName()
+ "? (Y/N): ");
System.out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
String response = in.readLine();
if (!response.equals("Y") && !response.equals("y"))
throw new IOException("FileCopy: "
+ "existing file was not overwritten.");
} else {
String parent = toFile.getParent();
if (parent == null)
parent = System.getProperty("user.dir");
File dir = new File(parent);
if (!dir.exists())
throw new IOException("FileCopy: "
+ "destination directory doesn't exist: " + parent);
if (dir.isFile())
throw new IOException("FileCopy: "
+ "destination is not a directory: " + parent);
if (!dir.canWrite())
throw new IOException("FileCopy: "
+ "destination directory is unwriteable: " + parent);
}
FileInputStream from = null;
FileOutputStream to = null;
try {
from = new FileInputStream(fromFile);
to = new FileOutputStream(toFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = from.read(buffer)) != -1)
to.write(buffer, 0, bytesRead); // write
} finally {
if (from != null)
try {
from.close();
} catch (IOException e) {
;
}
if (to != null)
try {
to.close();
} catch (IOException e) {
;
}
}
}
private static void Delete() {
String fileName = "fromFile.txt";
// A File object to represent the filename
File f = new File(fileName);
boolean success = f.delete();
}
}