Loops 循环,使用意外结果进行迭代,属性
*该代码给出了以下结果: 下一页1:Filnumber1,Filnumber2…Filenumber10 下一页2:Filenumber11,Filenumber12..,Filenumber20 下一页5:Filenumber41,Filenumber42..,Filenumber46 等等。我期待结果,所以,如果我下一次和舍多勒一起开始,它应该开始了 使用nextpage2并打印11-20中的文件 如果我再次启动程序,它应该从下一页3开始,并打印21-30之间的文件,依此类推,这取决于totalResults的值 解决方案可能是将值保存在属性中,使其持久化,以便 如果我再次运行该程序,它将读取属性config.properties以从正确的索引开始,但我不知道如何通过循环进行迭代Loops 循环,使用意外结果进行迭代,属性,loops,properties,Loops,Properties,*该代码给出了以下结果: 下一页1:Filnumber1,Filnumber2…Filenumber10 下一页2:Filenumber11,Filenumber12..,Filenumber20 下一页5:Filenumber41,Filenumber42..,Filenumber46 等等。我期待结果,所以,如果我下一次和舍多勒一起开始,它应该开始了 使用nextpage2并打印11-20中的文件 如果我再次启动程序,它应该从下一页3开始,并打印21-30之间的文件,依此类推,这取决于tot
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();
}
}
这是does make循环和打印输出
下一页1,文件编号1,文件编号2,…,文件编号10
然后将下一页值保存到属性文件中
如果你重新开始,它会打印出来
下一页2,文件编号11,文件编号12,…,文件编号20
您应该有一个名为config.properties的e属性文件
但是键nextPage和值1,nextPage=1;-->config.properties你为什么期望这样?首先将新值写入属性文件。循环应在第一页之后停止,并应将此值保存到属性文件中。如果我运行程序,它应该启动如果2页?我不知道怎么解决??
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;
}
}
}