java - 在我制作的 Java 程序中创建文件之前如何创建/修改文件的内容?

标签 java eclipse file java-io

嘿,这是我的代码,我创建了一个简单的文件创建程序,因为我在过去 2 天里只使用过 Java。我只有 13 岁,所以请简单点:)

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Filecreator 
{
    public static void main( String[] args )
    {   
        Scanner read = new Scanner (System.in);
        String y;
        String u;


        try {
            System.out.println("Please enter the name of your file!");

            y = read.next();

            while (y.contains(".") || y.contains(",") || y.contains("{") || y.contains("}") || y.contains("@")){
                System.out.println("Your Filename contains an incorrect character you may only use Number 0-9 And Letters A-Z");
                System.out.println("Please Re-enter your file name");

                y = read.next();          
            }

            System.out.println("Please enter the file type name");

            u = read.next();

            while (u.contains(".") || u.contains(",") || u.contains("{") ||     u.contains("}") || u.contains("@") ){
                System.out.println("Your File-type name contains an incorrect character      you may only use Number 0-9 And Letters A-Z");
                System.out.println("Please Re-enter your file-type name");

                u = read.next();
            }


            File file = new File( y + "." + u );


            if (file.createNewFile()){
                System.out.println("File is created!");
            System.out.println("The name of the file you have created is called " + y +   file);
            }else{
                System.out.println("File already exists.");
            } 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

如果您在 Eclipse 等程序上运行它,您将看到输出。但是我希望能够在最终选择名称和文件类型然后保存之前编辑[文件]的内容。无论如何我可以做到这一点吗?谢谢 - 乔治

最佳答案

目前您正在将所有内容打印到控制台 - 这是在您使用 System.out.println(...) 时完成的。

您可以做的是将输出写到别处。你怎么能做到这一点?最简单的方法是使用 StringBuilder:

http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

这是一个代码示例:

StringBuilder sb = new StringBuilder();
sb.append("one ");
sb.append("two");
String output = sb.toString(); // output contains string "one two"

现在您的整个输出都在一个字符串中。如果您查看 StringBuilder 文档(上面的链接),您会发现还有其他有用的方法,如插入或删除,可帮助您在将输出转换为字符串(使用 toString 方法)之前修改输出。完成所有修改后,您可以将此字符串写入文件。

对于将字符串写入文件,这可能会有所帮助:

How do I save a String to a text file using Java?

如果您正在编写小文件(最多几 MB),这种方法就足够了。如果你想写更大的文件,你不应该在将它写入文件之前将整个字符串存储在内存中。在这种情况下,您应该创建较小的字符串并将它们写入文件。有一个很好的教程:

http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/

关于java - 在我制作的 Java 程序中创建文件之前如何创建/修改文件的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20767984/

相关文章:

java - 编译 GWT 项目时出现错误 : Could not find or load main class com. google.gwt.dev.Compiler

java - 使用 http ://xyz. abc.com:9000 访问 Sonarqube

Java:查找具有最高值的字符串

java - 我应该将我的实体 ID 放在 URL 中还是作为隐藏字段放入表单中?

java - Android Glide 库 - 参数不能为空

eclipse - 如何构建 Eclipse RCP 应用程序以便其功能可以自动更新?

更改默认 mkstemp 的文件权限

java - 用于读取 Java 文件的最好/最简单的类是什么?

C编程: reading and writing binary files

java - 获取所有可以处理 Intent.ACTION_MEDIA_BUTTON Android 13 API33 的 android 包