[英]how do i write a batch file that i already have made to a specific directory in Java?
I am trying to make this program that will write a file to the users computer but am having trouble really, I want to write a file called desktop.bat
that I have to the c:/
directory but it doesn't seem like it's working. 我正在尝试制作一个程序,该程序将文件写入用户计算机,但确实遇到了麻烦,我想编写一个名为
desktop.bat
的文件,该文件必须位于c:/
目录下,但似乎无法正常运行。 this is the code: 这是代码:
package javawriter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class JavaWriterObject {
public void TryThis(){
try{
File file = new File("C:\\Desktop.bat");
if (file.exists()){
System.out.println("The file exists \ndirectory is found");
}else{
System.out.println("file is not found yet ".concat("file will be created"));
file.createNewFile();
}
FileWriter out = new FileWriter(file);
BufferedWriter writer = new BufferedWriter(out);
writer.write();
writer.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
Do I have to write a String for the writer.write();
我是否必须为
writer.write();
写一个字符串writer.write();
or can I do something where I can write the file I want instead? 还是可以做一些我可以写我想要的文件的事情?
Java 7's Files.copy
method helps you copy a file from one location to another . Java 7的
Files.copy
方法可帮助您将文件从一个位置复制到另一位置 。
If you're using an older version of Java, you will need to either read the original file in yourself and copy its contents into your new file, or you'll have to use a third-party library. 如果您使用的是Java的旧版本,则需要自己读取原始文件并将其内容复制到新文件中,或者必须使用第三方库。 See the answers to this question for several good solutions, including using Apache Commons IO
FileUtils
or just the standard Java API. 请参阅此问题的答案以获取几种好的解决方案,包括使用Apache Commons IO
FileUtils
或仅使用标准Java API。
Once you've decided how you want to copy the file, you might run into another problem. 确定要复制文件的方式后,可能会遇到另一个问题。 By default, Windows 7 will prevent you from writing to certain directories such as
C:\\
. 默认情况下,Windows 7将阻止您写入某些目录,例如
C:\\
。 You can try writing to a different directory, such as in the temp directory or any location within the user's home directory. 您可以尝试写入其他目录,例如temp目录或用户主目录中的任何位置。 If you must write to C:\\, the easiest solution (aside from creating the file ahead of time in Windows and overwriting it in your program, which probably defeats the purpose) is to disable UAC and make sure your user account has write permission on that directory--but this, of course, has security implications.
如果您必须写入C:\\,最简单的解决方案(除了在Windows中提前创建文件并在程序中覆盖文件,这可能会破坏目的)是禁用UAC,并确保您的用户帐户对该目录-但这当然具有安全隐患。
You have to get the content you want to write from somewhere , either a String literal in your application, or, the preferred method would be from a resource (bundled along with your application) via. 您必须从某处获取要编写的内容,或者是应用程序中的String文字,或者首选方法是通过(与应用程序捆绑在一起)资源。 YourClass.class.getResourceAsStream(name) .
YourClass.class.getResourceAsStream(name) 。
Copying an input stream to a file is a bit of effort, Guava can copy , but, if you're using Guava, you may as well copy directly from the resource to the file . 将输入流复制到文件有点费力, Guava可以复制 ,但是,如果您使用的是Guava,则最好直接从资源复制到文件 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.