简体   繁体   English

如何将已经制作的批处理文件写入Java中的特定目录?

[英]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.

相关问题 Java - 如何将文件写入指定的目录 - Java - how do I write a file to a specified directory 在用Java程序创建文件之前,如何创建/修改文件的内容? - How do i create/modify the contents of a file before i create it in a Java program i have made? 如何使用自己制作的方法? 爪哇 - How do I use a method I have made? JAVA 如何使用批处理文件在 Java 中进行编译? - How do I compile in Java with a batch file? 在我制作的另一个程序中按一个按钮后,如何使已创建的程序运行? - How do I make a program that I have already created run after I press a button in a different program that I have made? 如何制作新的主 class? 已经制作了一个,但不是自己的。java 文件,目前无法使用 - How do I make a new main class? There is already one made but its not its own .java file and currently does not work 如何写入已经运行的Java程序的输入流? - How do I write to the input stream of an already running java program? 如何在Java目录及其子目录中查找/列出具有特定名称的文件或目录? - How do I find/list a file or directory with specific name inside a directory and its subdirectories in Java? 如何将对象写入已存在的文件? - How do I write an object to a file that already exists? Spring 批处理,如何将简单字符串写入文件? - Spring batch, how do I write simple strings into a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM