What i am trying to do is have my java create a vbs file, open the file to run it, than delete it. i have covered the creating and deleting part of it but what i am trying to do it run it, and since eventually the jar will be in random places i cant open it with an exact path. Anyone have any ideas on how to accomplish this? Here is what i have (simplified for reading)
public void MapDrive() throws IOException {
File map = new File("map.vbs");
map.createNewFile();
PrintWriter writer = new PrintWriter("map.vbs");
writer.println(" VBS code here ");
writer.close();
map.delete();
}
Related
I am trying to write files to my windows 7 computer using IntelliJ IDEA. I am using the File and Filewriter programs to do this. But I am receiving an error message claiming to not have access to my folders in order to do this.
I have tried looking at other tutorials and people with a similar issue but I have not seen anyone with this issue so far. I have also looked at IntelliJ's permissions in the firewall and they are all in check. I also tried using different derectories such as my SRC folder and others, to no prevail.
public class Main {
public static void main(String[] args) throws IOException {
//fori loop
for(int a=0;a<1000;a++) {
//writing to desktop
File file = new File("C:\\Users\\BlahBlah\\Desktop\\");
FileWriter fw = new FileWriter(file);
fw.write("Hey you!");
fw.close();
}
}
}
I should expect a outflow of 1000 files to be written to my pc but instead I get an error telling me "Access is denied". The entire error is listed below.
Exception in thread "main" java.io.FileNotFoundException: C:\Users\BlahBlah\Desktop (Access is denied)
The exception is clear, it's telling you that there is not file there. Indeed C:\Users\BlahBlah\Desktop is not a file path, you should have something like:
file = new File("C:\\Users\\BlahBlah\\Desktop\\test.txt");
And you're creating a File 1000 times, I think that you might have an error there as well.
Try adding index a value to the filename with an extension say desktop1.txt
for(int a=0; a<1000;a++)
{
FileWriter fw=new FileWriter("D:\\ desktop"+a+".txt");
fw.write("hey file."+a);
fw.close();
}
I am working on a game in Java and I have a class that reads an audio file as InputStream and then plays that file through AudioPlayer.
I keep getting a file not found exception.
I have tried placing the audio files in many different locations, and nothing has worked.
This is my class code:
public void play(String string,int sleep) throws IOException
{
//this part does not recognize file
try {
//System.out.println(System.getProperty());
AS = new AudioStream(new FileInputStream(string));
AD = AS.getData();
loop = new ContinuousAudioDataStream(AD);
}
catch(IOException error){
System.out.print(string +" file not found");
}
AP.start(loop);
}
I pass a string like ("audio/beginning.wav")
You are using a path that is relative to the working directory defined when your program runs.
If you are running the program from Eclipse, by default it's the root directory of your program (top-level folder of the program). So normally placing the file in program_folder/audio/beginning.wav should work.
However, the working directory can be changed in the Run Configuration that you are using in Eclipse to run the program: go to the tab Arguments, and you'll find Working directory.
To debug the problem, check the output of the following:
System.out.println(new File("").getAbsolutePath());
Another option is to use the absolute path of the file.
This may be a stupid question, but I have to ask because I couldn't find any proper solution.
I am new to Eclipse. I created a Dynamic Web project in Eclipse, In this, I write a simple code to create a text file, Only file name is specified Not the path that where to create, After successful execution, i could not find my text file in my project folder.
If path is specified in the code, I can find the text file in specified directory, My Question is where i can find my text file if i am not specify a path ?
And my code is
try {
FileWriter outFile = new FileWriter("user_details.txt", true);
PrintWriter out1 = new PrintWriter(outFile);
out1.append(request.getParameter("un"));
out1.println();
out1.append(request.getParameter("pw"));
out1.close();
outFile.close();
System.out.println("file created");
} catch(Exception e) {
System.out.println("error in writing a file"+e);
}
I edited my code with following lines,
String path = new File("user_details.txt").getAbsolutePath();
System.out.println(path);
The path that i got is below
D:\Android\eclipse_JE\eclipse\user_details.txt
Why i got it in the eclipse folder ?
Then,
How can i create a text file in my web app, if this is not the right way to create a textfile ?
The file is located in the actual working directory of your application server. Do a
System.out.println(new File("").getAbsolutPath());
and you'll find the location.
However this is not a good idea to write files in web application like this, because first you never know where it is and second you never know whether you write privilege on it.
You need to specify some filesystem root for your application by passing it as init-parameter and use it as parent for everything you need to do on the filesystem. Check this answer to a similar Question.
You could then create your file like this:
String fsroot = getServletContext().getInitParameter("fsroot")
File ud = new File(fsroot, "user_details.txt");
FileWriter outFile = new FileWriter(ud, true);
You may try the getAbsolutePath() method.
String newFile = new File("Demo.txt").getAbsolutePath();
It will show the location where the files will be created.
This code creates a .txt in the folder directory (it works) but when comes the time to delete the whole directory or the .txt file using delete() method, nothing happens.
The delete() method works only when I replace the .txt file with an ordinary folder
import java.io.*;
public class Filemkdir {
public static void main(String[] args) throws Exception {
File f = new File("C:/Temp/Java/secret.txt");
FileWriter fSecret = new FileWriter(f);
f.mkdir();
f.delete();
}
}
On Windows you can't delete an open file. Close the FileWriter first.
Also, the
f.mkdir();
seems completely pointless.
Maybe the fSecret (FileWriter) needs to be closed first. Otherwise the file is "in use"
fSecret.close();
You would basically need to close the writer object before deleting the file
File delete work on file, if you are trying to delete directory you first need to delete all files inside id.
with your code you are giving path for file, but you are creating directory after that.
I know it's been asked before and answered, but i just can't make it work.
So, my files hierarchy is:
+ Project
+ build.xml
+ save.xml
+ src
+ build
I have the "save" file to save the state of a game in a given instant. It should be easy to overwrite and easy to read to load everything again in the game.
My save() is like this, and it seems to be working:
public void save(Game game) throws IOException{
Document doc = DocumentHelper.createDocument();
doc.add(game.save());
File save=null;
save = new File("./save.xml");
FileWriter writer = new FileWriter(save);
doc.write( writer);
writer.close();
}
game.save() is a method in the game that actually does what i want to do. It does it recursively and all, and i know it works fine because i opened the XML file with another program.
So, my problems begins here. My getinfofromxml() method is:
public Game getinfofromxml() throws IOException{
Game game;
SAXReader reader = new SAXReader();
try{
URL fileWithData=getClass().getResource("./save.xml");
Document document = reader.read(fileWithData);
Element alreadySavedGame= document.getRootElement();
game= getGameSaved(alreadySavedGame);
}catch(DocumentException ex){
throw new IOException();
}
return game;
}
My problem is, when try to run it (via ant), no test will pass. I go to eclipse, and i can see that when i try to load the game, it throws Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException.
I've tried changing the line URL fileWithData=getClass().getResource("./save.xml"); to URL fileWithData=getClass().getResource("save.xml"); and URL fileWithData=getClass().getResource("../save.xml"); and things like that, but it says always the same.
Any idea?
Thank you for reading
so, if you use getResource() it will try to find your file using the classloader that was used to load that class. this might be (later at least) the jar file your app is packaged in, and i don't think that's you're trying to load your savegame from there :)
i'd load the file the same way you save it:
URL fileWithData= new File( "save.xml" ).toURI().toURL();
(you might have to catch an extra exception)