Deleting random access file in java - java

I've created a random access file as follows:
RandomAccessFile aFile = null;
aFile = new RandomAccessFile(NetSimView.filename, "rwd");
I want to delete the file "afile". can anyone suggest me how to do it?

You can do that:
File f = new File(NetSimView.filename);
f.delete();
Edit, regarding your comment:
The parameter NetSimView.filename seems to be a File and not a String that contains the path to the file. So simply do:
NetSimView.filename.delete();

Related

Trouble creating a folder then creating files within the folder

Hi I'm having a problem creating a folder in which every file I create in my for each loop will be placed. It is a basic problem but I can't seem to see it, any help would be much appreciated!
Scanner inputScan = new Scanner(System.in);
System.out.println("Enter location for output folder to be built..");
String filePath=inputScan.next();
inputScan.close();
File dir = new File(filePath+"subnet_output");
dir.mkdir();
for(String myAddr: addr){
String myFileName = myAddr.replaceAll("/", "-");
File file = new File(dir+myFileName+".txt");
PrintWriter writer = new PrintWriter(file, "UTF-8");
You are missing "/" while creating file inside folder:
File file = new File(dir+myFileName+".txt");
Replace with:
File file = new File(dir+File.pathSeparator+myFileName+".txt");
Try PrintWriter.append(...) and PrintWriter.flush() for actually writing into that file you want to create.
File file = new File(dir+"/"+myFileName+".txt");

Renaming a RandomAccessFile

i have a RAF called data.bin and a temporary RAF called temp.bin.
data = new RandomAccessFile("data.bin","rws");
temp = new RandomAccessFile("temp.bin","rws");
the temp file is basically the data.bin file but i alter the information in it. so once im done altering the temp file how do i change the name of temp.bin to data.bin and delete the old data.bin?
ive seen some things about renaming Files with .renameTo() and stuff but that does apply to a RandomAccessFile. and i couldnt find any method like that for RAFs
RandomAccessFile was not designed to replace the File class.
It was designed to allow reading an writing only well more or less.
Please use the File class to do the renaming.
Close RAF and use java.nio.file.Files.move to rename the temp file. File.renameTo has a draw back. If it fails to rename the file you will never know why since it just returns true or false. Files.move throws IOException if fails.
I had same problem.
I did something like this:
data.close();
temp.close();
File tempfile = new File("c:/.../temp.bin");
File datafile = new File("c:/.../data.bin");
tempfile.renameTo(datafile);
For more information: http://www.tutorialspoint.com/java/io/file_renameto.htm
RandomAccessFile purpose is to read/write information. To handle file names, existence, etc., you must use the File class.
In your case, you should do something like this:
File d = new File("data"); // Declare your data RAF through File
RandomAccessFile data = new RandomAccessFile(d,"rws");
public void foo(){
File t = new File("temp"); // Declare your temp RAF through File
RandomAccessFile temp = new RandomAccessFile(t,"rws");
//TODO data RAF alteration
data.close();
d.delete(); //First delete data File, just in case
temp.close();
File f = new File("data");
t.renameTo(f);
}

How to get the InputStream from a files list of rtf document files?

I have a list of richTextFormat documents that I need to loop through and get the InputStream from each file to merge with another document. I used this code to get the files:
List<File> rtfFilesList = new ArrayList<File>();
File[] files = new File(<path to files>).listFiles();
for (File file : files) {
if (file.isFile() && file.getName().endsWith(".rtf")) {
FilenameUtils.removeExtension(".rtf");
rtfFilesList.add(file);
}
}
That gets me the files fine, but now I want to loop through the list, as in:
for(File file : rtfFilesList){
//Get the document stream from each file here.
...
I don't know how to get the File cast to a specific type of file. API didn't seem to really support that.
Any ideas on this one?
Thanks,
James
Okay, I wasn't doing this quite right. What I really needed was:
for(File file : rtfTemplateList){
try {
fileInputStream = new FileInputStream(file);
byte[] byteArray = new byte[(int) file.length()];
fileInputStream.read(byteArray);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
etc...
What I found after writing down what I wanted to do was really get a byteArrayInputStream to pass to a third party software generator to create a merged rtf file. So using the file in a for loop was correct, but then I just needed to get the stream after that.
I hope this helps someone.

set directory to a path in a file

I just want to set the directory to a path I have written in a file before.
Therefore I used :
fileChooser.setCurrentDirectory(new File("path.txt"));
and in path.txt the path is given. But unfortunately this does not work out and I wonder why :P.
I think I got it all wrong with the setCurrentDic..
setCurrentDirectory takes a file representing a directory as parameter. Not a text file where a path is written.
To do what you want, you have to read the file "path.txt", create a File object with the contents that you just read, and pass this file to setCurrentDirectory :
String pathWrittenInTextFile = readFileAsString(new File("path.txt"));
File theDirectory = new File(pathWrittenInTextFile);
fileChooser.setCurrentDirectory(theDirectory);
You have to read the contents of path.txt. Thea easiest way is through commons-io:
String fileContents = IOUtils.toString(new FileInputStream("path.txt"));
File dir = new File(fileContents);
You can also use FileUtils.readFileToString(..)
JFileChooser chooser = new JFileChooser();
try {
// Create a File object containing the canonical path of the
// desired directory
File f = new File(new File(".").getCanonicalPath());
// Set the current directory
chooser.setCurrentDirectory(f);
} catch (IOException e) {
}

Change the name of the deepest directory in a File object

I have File object in Java which is a directory path:
C:\foo\foo\bar
...and I would like to change this to:
C:\foo\foo\newname
I'm don't mean renaming the actual directory, but, simply modifying the path in the File object. Could someone show me how I can do this? Do I have to use string functions for this or is there some inbuilt Java function that I can use?
Thanks.
You can construct one File from another and get the parent directory of a file, combining these:
File orig = new File("C:\\foo\\foo\\bar");
File other = new File(orig.getParentFile(), "newname");
There is no such method in java that changes path for the File object, however you can get the file path with getPath() or getAbsolutePath(). I think creating a new file at that path would do.
Try following:
import java.io.File;
public class MainClass {
public static void main(String[] a) {
File file = new File("c:\\foo\\foo\\bar");
file.renameTo(new File("c:\\foo\\foo\\newname"));
}
}
Hope this helps.
You can use the string-representation of the File object and search for the last / with indexOf(), then you change the value after it and create a new File object.
I guess you need to something like this.
String sourcePath = "C:\\foo\\foo\\bar";
String newName = "newname";
File source = new File(sourcePath);
File dest = new File(source.getParent() + File.separator + newName);
source.renameTo(dest);
I think you can only create a new File object with the new path:
File f2 = new File("C:\\foo\\foo\\newname")
Does it make any side effect on your code?

Categories