java - 创建属性文件时出现问题

标签 java

这就是我最终所做的。它工作得很好,但可能需要一些微调。

File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties");
FileInputStream inStream = null;
FileOutputStream outStream = null;
Properties config = new Properties();

        try{
            if (file.exists()){//Checks if it exists.
            inStream = new FileInputStream(file);
                if (inStream.available() >= 0){//Cheacks if it has anything in it.
                    config.load(inStream);
                    System.out.println(config);
                }
            }

            config.setProperty(property , score);//New property
            outStream = new FileOutputStream(file);
            config.store(outStream, "Property");//Names the Properties that are in the file Property
            config.list(System.out);//Prints out all the properties. 

        } catch (IOException ioe){//Handles any problems
            System.out.println("You just pooped the pants");
        } finally{//Closes both input and output Streams if they are open
            try {
                if(inStream != null)
                    inStream.close();
                if (outStream != null)
                    outStream.close();
            } catch (IOException e) {
            }
        }
}

我有两套代码。一种只是将属性写入文件,另一种则更深入一些。我认为如果填充不存在,他们都应该写,但只有其中一个存在。这是每个代码以及一些附加内容。我现在只是在使用控制台,我只是在闲逛,所以它一点也不华而不实。

private void properties() {
    System.out.println("What would you like to name the .properties file?");
    sTest.stringReader();
    String fileName =sTest.getString();// This will be the name of the file. 
    System.out.println("What property would you like to change?");
    sTest.stringReader();   
    String property= sTest.getString();
    System.out.println("What would you like to change the " + property + " to?");
    sTest.stringReader();
    String score = sTest.getString();

        try {
            File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties");
            FileInputStream inStream = new FileInputStream(file);
            Properties config = new Properties();
            config.load(inStream);
            // Create a new property
            config.setProperty(property , score);
            FileOutputStream outStream = new FileOutputStream(file);
            config.store(outStream, "Property");
            inStream.close();
            outStream.close();
            config.list(System.out);
        } catch (IOException ioe){
            System.out.println("Chould not write file.");
        }
}

这里只是编写一个属性,而不添加任何东西。这种方法是创建文件的方法,但我觉得“File file = new File”应该对这两种方法都执行此操作。但话又说回来,在编程时,感觉并不重要。我想要一个解释。谢谢。

private void myWrite() {
    System.out.println("What would you like to name your file?");
    sTest.stringReader();
    String fileName =sTest.getString();// This will be the name of the file. 
    System.out.println("What would you like to put in you file?");
    sTest.stringReader();
    String letsWrite = sTest.getString();
        try {
            File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties");
            FileOutputStream fileStream = new FileOutputStream(file);
            write(fileStream, letsWrite);
        } catch (IOException ioe){
            System.out.println("Could not write file" + ioe);
        }
    }

在您的帮助下重做代码:

System.out.println("What would you like to change the " + property + " to?");
    sTest.stringReader();
    String score = sTest.getString();

    try {
        File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties");
        FileInputStream inStream = new FileInputStream(file);
        Properties config = new Properties();
        config.load(inStream);
        inStream.close();
    } catch (IOException ioe){
        System.out.println("Chould not read file.");
    }

    try{
        File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties");
        FileOutputStream outStream = new FileOutputStream(file);
        Properties config = new Properties();
        config.setProperty(property , score);
        config.store(outStream, "Property");
        outStream.close();
    } catch (IOException ioe){
        System.out.println("Chould not write file.");
    }

它抛出 IOException 但它确实写入了文件。我如何在finally block 中关闭它?我还没用过那些。你如何先加载文件?我仍在努力,我只是想在你还在工作的时候把这个交给你。感谢您的帮助。

我还是不太明白这一点。我已经记录了我认为每件事都在做的事情。今晚晚些时候我会问我的 friend 发生了什么事,但他更像是一个 MatLab 专家。

File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties");//new instance of these file
    FileInputStream inStream = null; //Creates variable out side my try block
    FileOutputStream outStream = null;//Creates variable out side my try block
    Properties config = new Properties();//Creates variable out side my try block
    try {
        inStream = new FileInputStream(file); //Loads the file to the Input Stream
        config.load(inStream); //Loads the config with what was in the inStream.

    } catch (IOException ioe){//Handles any problems
        System.out.println("Chould not read file.");
    }

    try{
        //inStream = new FileInputStream(file);//Do I need to do this again? 
        //config.load(inStream);// Do I need to do this again? 

        //Creates a new property
        config.setProperty(property , score);

        outStream = new FileOutputStream(file); // Preps the outPut stream to write to the file
        config.store(outStream, "Property");//Names the Properties that are in the file Property
        config.list(System.out);//Prints out all the properties. 
    } catch (IOException ioe){//Handles any problems
        System.out.println("Chould not write file.");
    } finally{//Closes both input and output Streams
        try {
            inStream.close();//It says these need to be in a try/catch block also. 
        } catch (IOException e) {
        }
        try {
            outStream.close();
        } catch (IOException e) {
        }
    }

最佳答案

File 对象代表文件路径。创建 File 实例不会在文件系统上创建文件。打开 FileOutputStream 并向其写入是在文件系统上创建文件的操作。

您的第一个代码片段尝试同时读取和写入文件。您应该从文件中读取,关闭输入流,然后打开输出流,写入并关闭输出流。如果文件不存在,则读取该文件将抛出 IOException,因此您必须处理这种可能性。并且任何输入/输出流都应该始终在finally block 中关闭。

关于java - 创建属性文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6543988/

相关文章:

javascript - 需要关于过滤器图标编码的建议

java - Android 通过 WiFi 向 Java 客户端发送 UDP 数据包

java - 从两个列表中查找缺失的数字

java - 如何将我在不同类中创建的列表数组引用到另一个 2

java - 抑制 JAXB 生成类的编译器警告

java - 使用命令提示符和 BlueJ 编译时的不同结果

java - 无法在 Eclipse 中创建 GAE 实体

java - 当某个模式位于另一个模式内部时排除该模式

java - Spring Validation Framework 解决错误代码

java - 访问我的资源文件夹中的文件 Elastic Beanstalk/Spring Boot