循环,迭代意外的结果,属性

时间:2014-09-18 15:27:59

标签: loops properties

package javaapplication43;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
import java.io.FileInputStream;

public class JavaApplication43 {

    int totalResults = 45; //
    int itemsperPage = 10;
    int i = 0;
    int j = 0;
    int count = 0;

    FileOutputStream output = null;
    Properties prop = new Properties();
    FileInputStream input=null;

    public JavaApplication43() throws FileNotFoundException, IOException {

        output = new FileOutputStream("config.properties");

        // set the properties value
        prop.setProperty("totalResults", "45");
        prop.setProperty("itemsperPage", "10");
        prop.setProperty("?", "?");

        // save properties to project root folder
        prop.store(output, null);

        input = new FileInputStream("config.properties");

        // load a properties file
        prop.load(input);

        // get the property value and print it out
        System.out.println(prop.getProperty("totalResults"));
        System.out.println(prop.getProperty("itemsperPage"));
        System.out.println(prop.getProperty("?"));

    }

    public void makeLoop() {
        for (i = 1; i <= (totalResults / itemsperPage) + 1; i++) {
            System.out.println("nextPage " + i);
            for (; j < i * itemsperPage; j++) {
                if (j > totalResults) {
                    break;
                }
                System.out.println("Filenumber " + (j + 1));
            }
        }
    }

    public static void main(String[] args) throws IOException {
        JavaApplication43 myTest = new JavaApplication43();
        myTest.makeLoop();
    }
}

*本守则给出结果:

  • nextPage1:Filnumber1,Filnumber2 ... Filenumber10
  • nextPage2:Filenumber11,Filenumber12 ..,Filenumber20
  • nextPage5:Filenumber41,Filenumber42 ..,Filenumber46

等等。我期待结果如此,如果我下次开始使用一个脱毛器,它应该开始 使用nextpage2并打印11-20的文件,

如果我再次启动程序,它应该从下一页3开始并打印21-30的文件,依此类推取决于我对totalResults的值。

解决方案是可以保存属性中的值以使其持久化,以便 如果我再次运行Programm,它将读取Property config.properties以在正确的索引上启动,但我不知道如何通过循环进行迭代。 ?

1 个答案:

答案 0 :(得分:0)

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
import java.io.FileInputStream;

public class JavaApplication43_with_Main_3 {

    public static void main(String[] args) throws IOException {
        int totalResults = 45; //
        int itemsperPage = 10;
        int i = 0;
        int j = 0;
        FileOutputStream output = null;
        Properties prop = new Properties();
        FileInputStream input = null;
        input = new FileInputStream("config.properties");


        // load a properties file
        prop.load(input);


        // get the property value and print it out
        System.out.println("nextPage Prop " + prop.getProperty("nextPage"));

        String nextPage = prop.getProperty("nextPage");
        int intNextPage = Integer.parseInt(nextPage);
        System.out.println("intNextPage " + intNextPage);

        for (i = intNextPage; i <= (totalResults / itemsperPage) + 1; i++) {
            int jNextPage=intNextPage-1;
            System.out.println("nextPage here " + i);
            for (j=jNextPage*itemsperPage; j < i * itemsperPage; j++) {
               // System.out.println("j ist "+j);
                if (j > totalResults) {
                    break;
                }
                System.out.println("Filenumber " + (j + 1));
            }
            String strI = "" + (i + 1);
            System.out.println("hello " + strI);
            output = new FileOutputStream("config.properties");
            prop.setProperty("nextPage", strI);
            prop.store(output, null);
            break;
        } 
    }
}

这是make循环并打印出

  • nextPage 1,Filenumber1,Filenumber2,..,Filenumber10

然后将nextPage值保存到属性文件中。

如果再次开始,它会打印

  • nextPage 2,Filenumber11,Filenumber12,...,Filenumber20

你应该拥有名称为config.properties的e Propertiy文件 但是键nextPage和值1,nextPage = 1; ---&gt; config.properties