This doesn't seem to create a file or folder. Why?
import java.io.*;
public class file1
{
public static void main(String[] args)
{
File text1 = new File("C:/text1.txt");
File dir1 = new File("C:/dir");
}
This one below does create a file.
import java.io.*;
public class file3
{
public static void main(String[] args)
{
try
{
FileWriter text1 = new FileWriter("C:/text.txt");
FileWriter dir = new FileWriter("C:/dir");
}
catch(Exception e){}
}
}
However, the directory seems to have a strange unusable icon.
What can I do to create a directory.
What are other simple methods to create files and folders.
Surprisingly, the File class does not represent a file. It actually represents a pathname for a file ... that may or may not exist.
To create a file in Java, you need to open it for output; e.g.
File text1 = new File("C:/text1.txt");
FileOutputStream os = new FileOutputStream(text1); // The file is created
// here ... if it doesn't
// exist already.
// then write to the file and close it.
or you could do this - new FileOutputStream("C:/text1.txt"). In both cases, an existing file will be truncated ... unless you use the FileOutputStream with a boolean parameter that says open for appending.
If you want to create a file without writing any data to it, you could also do this:
File text1 = new File("C:/text1.txt");
text1.createNewFile();
However, that will only create a new file if the file didn't already exist.
To create a directory in Java, use the File.mkdir() or File.mkdirs() methods.
UPDATE
You commented:
I tried File dir = new File("C:/dir1").mkdir(); it says incompatible types.
That is right. The mkdir() method returns a boolean to say whether or not it created the directory. What you need to write is something like this:
File dir = new File("C:/dir1");
if (dir.mkdir()) {
System.out.println("I created it");
}
Always READ THE JAVADOCS before using a method or class you are not familiar with!
A couple more things you need to know:
The best way to deal with the problem of making sure a file gets closed is to do something like this:
try (FileOutputStream os = new FileOutputStream(text1)) {
// now write to it
}
The stream os will be closed automatically when the block exits.
It is usually "bad practice" to catch Exception. It is always "bad practice" to catch Exception and do nothing in the handler. This kind of this hides the evidence of bugs, and makes your code unpredictable and hard to debug.
If you're creating a directory with File, you want this:
new File("C:/dir").mkdirs();
For creating directory you can use :
if(!text1.exists()){
text1.mkdir();
}
and for creating file use:
if(!text1.exists()){
try {
text1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Related
Hi I want to know how the program can locate a file.
For example. I have a class
public class MiReader {
private File file;
private BufferedReader bufferedReader;
public MiReader(String dir) {
try {
file= new File(dir);
bufferedReader = new BufferedReader(new FileReader(file));
} catch (Exception ex) {
Logger.getLogger(MiReader.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void imprimir() {
***
}
}
I know that the file is on the project (I'm using netbeans)
project is on C:\NetBeansProjects\Application
file: C:\NetBeansProjects\Application\file.txt
so when I instance MiReader must be something like this:
MiReader mr = new MiReader("C:\\NetBeansProjects\\Application\\file.txt");
and now if I run the program from another location
for example now its on
D:\Pograms\Application
so the file is D:\Pograms\Application\file.txt
and now I have to change the way I create the class to
MiReader mr = new MiReader("D:\\Pograms\\Application\\file.txt");
I want to know how the program can locate the file just running the program,
something like
MiReader mr = new MiReader(program.getLocation()+"\\file.txt")
Learning english :)
You could use relative paths. Aka
MiReader Mr = new MiReader("file.text");
This way the program will look for the file file.text inside the directory you run it from.
You can use System.getProperty to get the user.home, the user.dir, the classpath etc as a standard prefix for the file you are trying to open. Here are all of the System properties
ie
File f = new File (System.getProperty("user.home" + "/foo.txt"));
I don't understand how to use TextIO's readFile(String Filename)
Can someone please explain how can I read an external file?
public static void readFile(String fileName) {
if (fileName == null) // Go back to reading standard input
readStandardInput();
else {
BufferedReader newin;
try {
newin = new BufferedReader( new FileReader(fileName) );
}
catch (Exception e) {
throw new IllegalArgumentException("Can't open file \"" + fileName + "\" for input.\n"
+ "(Error :" + e + ")");
}
if (! readingStandardInput) { // close current input stream
try {
in.close();
}
catch (Exception e) {
}
}
emptyBuffer(); // Added November 2007
in = newin;
readingStandardInput = false;
inputErrorCount = 0;
inputFileName = fileName;
}
}
I had to use TextIO for a school assignment and I got stuck on it too. The problem I had was that using the Scanner class I could just pass the name of the file as long as the file was in the same folder as my class.
Scanner fileScanner = new Scanner("data.txt");
That works fine. But with TextIO, this won't work;
TextIO.readfile("data.txt"); // can't find file
You have to include the path to the file like this;
TextIo.readfile("src/package/data.txt");
Not sure if there is a way to get it to work like the Scanner class or not, but this is what I've been doing in my course at school.
The above answer (about using the correct file name) is correct, however, as a clarification, make sure that you actually use the proper file path. The file path suggested above, i.e. src/package/ will not work in all circumstances. While this will be obvious to some, for those of you who need clarification, keep reading.
For example (and I use NetBeans), if you have already moved the file into NetBeans, and the file is already in the folder you want it to be in, then right click on the folder itself, and click 'properties'. Then expand the 'file path' section by clicking on the three dots next to the hidden file path. You will see the actual file path in its entirety.
For example, if the entire file path is:
C:\Users..\NetBeansProjects\IceCream\src\icecream\icecream.dat
Then, in the java code file itself, you can write:
TextIo.readfile("src/icecream/icecream.dat");
In other words, make sure you include the words 'src' but also everything that follows the src as well. If it's in the same folder as the rest of the files, you won't need anything prior to the 'src'.
I am looking for a away to rename a file to a string. renameTo only takes another file as a parameter, but I want it to take a string. So basically, how do I implement this method here?
public static void renameFile(File toBeRenamed, String new_name) {
}
I would like to rename the file "toBeRenamed" to "new_name". Do I have to make another file called new_name, or is there some workaround? Thanks!
EDIT: Thanks for the answer Luiggi. Here is a pic of the new error:
The File class doesn't represent the physic file in the hard drive, it is just an abstract representation. Creating a new instance of File class doesn't mean you are creating a physical file.
By knowing this, you can rename your file using a new File without worrying about creating new physical files. Code adapted from Rename a file using Java:
public static void renameFile(File toBeRenamed, String new_name)
throws IOException {
//need to be in the same path
File fileWithNewName = new File(toBeRenamed.getParent(), new_name);
if (fileWithNewName.exists()) {
throw new IOException("file exists");
}
// Rename file (or directory)
boolean success = toBeRenamed.renameTo(fileWithNewName);
if (!success) {
// File was not successfully renamed
}
}
EDIT: Based on your question update and on this comment:
I took a pic of the error. "Unhandled Exception Type IO Exception"
Looks one of these:
You don't know how to handle checked exceptions.
To do this, you should wrap the method that throws the Exception (or subclass) in a try-catch statement:
String new_name = getFilename(file);
try {
renameFiles(files[i], new_name);
} catch (IOException e) {
//handle the exception
//using a basic approach
e.printStacktrace();
}
More info: Java Tutorial. Lesson: Exceptions.
You don't want your method to throw a checked exception. In this case, it would be better to throw an unchecked exception instead, so you don't need to handle the exception manually. This can be done by throwing a new instance of RuntimeException or a subclass of this:
public static void renameFile(File toBeRenamed, String new_name) {
File fileWithNewName = new File(new_name);
if (fileWithNewName.exists()) {
throw new RuntimeException("file exists.");
}
// Rename file (or directory)
boolean success = toBeRenamed.renameTo(fileWithNewName);
if (!success) {
// File was not successfully renamed
}
}
More info in the link posted in the above section.
You don't want to throw an exception at all. In this case, it would be better to at least return a value to know if the file was exactly renamed:
public static boolean renameFile(File toBeRenamed, String new_name) {
//need to be in the same path
File fileWithNewName = new File(toBeRenamed.getParent(), new_name);
if (fileWithNewName.exists()) {
return false;
}
// Rename file (or directory)
return toBeRenamed.renameTo(fileWithNewName);
}
And update your code accordingly:
String new_name = getFilename(file);
boolean result = renameFiles(files[i], new_name);
if (!result) {
//the file couldn't be renamed
//notify user about this
System.out.println("File " + files[i].getName() + " couldn't be updated.");
}
Which one to choose? Will depend entirely on your taste. If I were you, I would use the third option for a quick dirty or learning phase work, but for a real world application I would use second option but using my own custom exception that extends from RuntimeException.
Perhaps this could be useful for you
// File (or directory) with old name
File file = new File("oldname");
// File (or directory) with new name
File file2 = new File("newname");
if(file2.exists()) throw new java.io.IOException("file exists");
// Rename file (or directory)
boolean success = file.renameTo(file2);
if (!success) {
// File was not successfully renamed
}
This is extracted from a similar question Rename a file using Java
In a project I'm working on in Windows 7, using JDK 7u25, I have a class that stores the state for the application. That class has a static save method to save the state to disk. I originally had the following code. (state is a JSONObject and I'm assuming that the output directory hasn't necessarily been created when this function is called.)
public State {
private static String stateFile = "\\state\\state.json";
public static void save() {
try {
File sFile = new File(stateFile);
sFile.mkdirs();
sFile.setWritable(true);
FileWriter file = new FileWriter(sFile);
String str = state.toString(4);
file.write(str);
file.close();
} catch (IOException ex) {
HLogger.log(ex.getMessage());
}
}
}
Whenever I ran this code, it logged an IOException - complaining that access to the state file was denied. I discovered that if I changed the save function (as shown below) it would work exactly as expected.
public static void save() {
try {
File sFile = new File(stateFile);
File path = new File(sFile.getParent());
path.mkdirs();
sFile.setWritable(true);
FileWriter file = new FileWriter(sFile);
String str = state.toString(4);
file.write(str);
file.close();
} catch (IOException ex) {
HLogger.log(ex.getMessage());
}
}
Can anyone explain why it's necessary to create the output directory in a separate file object in order to obtain write permission for the file?
This is not permissions failure but is a misuse of FileWriter.
In the first code snippet a directory named state.json is being created by the call to mkdirs() because it:
Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.
and then an attempt to write to the directory using a FileWriter is made, which fails. The following is the throws clause From FileWriter(File):
IOException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason
The second snippet creates a directory named state (because it is using the parent of \\state\\state.json) and then a file named state.json, and therefore uses FileWriter on a regular file which succeeds.
i am trying to create a text file in a folder (called AMCData). The file is called "File" (for the sake of this example).
I have tried using this code:
public static void OpenFile(String filename)
{
try
{
f = new Formatter("AMCData/" + filename + ".txt");
}
catch(Exception e)
{
System.out.println("error present");
}
}
But before i get the chance to even place any text in it, the catch keeps being triggered..
Could anyone inform me why this is occuring?
more information:
The folder does not exist, i was hoping it would automatically create it
If it doesn't automatically create folders, could you please link me to how to do so?
You're right, a Formatter(String) constructor needs the file to be present or createable. The most likely reason why a file cannot be created is that it references a folder that itself doesn't exist, so you should use the File.mkdirs() method, like this:
new File("AMCData").mkdirs();