创建属性文件时出现问题

时间:2011-07-01 05:36:41

标签: 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.");
        }
}

这里只是写一个属性而不添加任何东西。这个方法是创建文件的方法,但我觉得“文件文件=新文件”应该为两者做。但是再次感受在编程时不会占很多。我喜欢解释。谢谢。

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块中关闭它?我还没用过那些。你如何首先加载文件?我还在努力,我只想在你还在的时候给你这个。谢谢你的帮助。

我仍然没有得到这么好。我已经记录了我认为一切正在做的事情。我今晚晚些时候会问我的朋友发生了什么,但他更像是一个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) {
        }
    }

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

代码抛出IOException,因为config.load()从文件中读取。但是,由于您正在处理异常,因此代码在此时不会失败并继续写入文件。如果您先写入文件然后从中读取,则不会得到异常。如果文件上有数据,您可以先加载文件。

    File file = new File("C:/Users/Mike Home/Desktop/"+fileName+".properties");
    FileInputStream inStream = new FileInputStream(file);
    if(inStream.available() > 0){
        Properties config = new Properties();
        config.load(inStream);
    }
    inStream.close();

上述代码仅在流中有数据时才会读取,因此不会抛出异常