如何保存文件和阅读 [英] How to save file and read
问题描述
我创建了一个将网格布局格式的随机图像放置的程序。
网格布局的大小是6 x 6 = 36.
只有10个图像被填满(每个图像都不同),其余都是空的。
alt text http://freeimagehosting.net/uploads/bfb7e85f63.jpg
如何将其保存到文件并再次读取,以便在网格上显示相同图像的相同图像?
以下是我用来保存图像的代码:
//图像文件
String [] arrPic = {pic1.jpg,pic2.jpg,pic3.jpg,pic4.jpg,pic5.jpg,pic6.jpg,pic7.jpg,pic8 .JPG, pic9.jpg, pic10.jpg ,, pic11.jpg, pic12.jpg, pic13.jpg};
ArrayList< String> pictures = new ArrayList< String>(Arrays.asList(arrPic));
ArrayList< String> file = new ArrayList< String>();
JPanel pDraw = new JPanel(new GridLayout(6,6,2,2));
...
//用空标签填充所有网格
for(int i = 0; i <(6 * 6); i ++){
JLabel lbl = new JLabel();
pDraw.add(lbl);
}
...
//选择随机框以填充图像
(int i = 0; i <10; i ++){
布尔数=真;
while(number){
int n = rand.nextInt(35);
if(!(arraylist.contains(n)))
number = false;
arraylist.add(n);
}
//用图像填充网格
for(int i = 0; i< arraylist.size(); i ++){
//从arraylist
中选择随机图像int index = rand.nextInt(pictures.size());
String fileName =(String)pictures.get(index);
//找到图片文件
icon = createImageIcon(fileName);
//将文件保存到新文件中
file.add(fileName);
//重新缩放图片
int x = rand.nextInt(50)+50;
int y = rand.nextInt(50)+50;
Image image = icon.getImage()。getScaledInstance(x,y,Image.SCALE_SMOOTH);
icon.setImage(image);
//删除空标签并将其替换为图像
int one =(Integer)arraylistist.get(i);
pDraw.remove(one);
final JLabel标签;
pDraw.add(label,one);
解决方案在你的代码,是类 java.util.Random
的 rand
?如果是这样,你可以自己种下,然后将种子值保存到文本文件中。对于任何给定的种子,(伪)随机数发生器将以相同的顺序产生相同的随机数字。所以:
随机rand = null;
然后,创建一个新的种子并将其保存到文件中:
long seed = System.currentTimeMillis();
rand = new Random(seed);
尝试{
BufferedWriter writer = new BufferedWriter(new FileWriter(whatever.txt));
writer.write(Long.toString(seed));
writer.close();
} catch(IOException e){
e.printStackTrace();
}
或者将以前保存的值读回:
long seed = 0;
尝试{
BufferedReader reader = new BufferedReader(new FileReader(whatever.txt));
seed = Long.parseLong(reader.readLine());
reader.close();
} catch(IOException e){
e.printStackTrace();
}
rand = new Random(seed);
请注意,此代码不会检查以确保文件退出,或文件isn不是空的,或者文件不包含有效数字以外的内容等。
I create a program that place random image in grid layout format.
The size of the grid layout is 6 x 6 = 36.
Only 10 were filled with images (each image was different) and the rest were empty.
alt text http://freeimagehosting.net/uploads/bfb7e85f63.jpg
How can I save it to a file and read it again, so it will display the same images with same placement on the grid?
Here is the code that I used to save the images:
//image file
String []arrPic = {"pic1.jpg","pic2.jpg","pic3.jpg","pic4.jpg","pic5.jpg","pic6.jpg","pic7.jpg","pic8.jpg","pic9.jpg","pic10.jpg",,"pic11.jpg","pic12.jpg","pic13.jpg"};
ArrayList<String> pictures = new ArrayList<String>(Arrays.asList(arrPic));
ArrayList<String> file = new ArrayList<String>();
JPanel pDraw = new JPanel(new GridLayout(6,6,2,2));
...
//fill all grids with empty label
for (int i =0; i<(6*6); i++){
JLabel lbl = new JLabel("");
pDraw.add(lbl);
}
...
//Choose random box to be filled with images
for(int i=0; i<10; i++){
Boolean number = true;
while(number){
int n = rand.nextInt(35);
if(!(arraylist.contains(n)))
number = false;
arraylist.add(n);
}
//fill the grids with images
for(int i=0; i<arraylist.size(); i++){
//select random image from arraylist
int index = rand.nextInt (pictures.size());
String fileName = (String) pictures.get(index );
//find the image file
icon = createImageIcon(fileName);
//save the file in a new file
file.add(fileName);
//rescaled the image
int x = rand.nextInt(50)+50;
int y = rand.nextInt(50)+50;
Image image = icon.getImage().getScaledInstance(x,y,Image.SCALE_SMOOTH);
icon.setImage(image);
//remove empty label and replace it with an image
int one = (Integer) arraylist.get(i);
pDraw.remove(one);
final JLabel label;
pDraw.add(label,one);
}
解决方案 In your code, is rand
of the class java.util.Random
? If so, you could "seed" it yourself, and then just save the seed value to a text file. For any given seed, a (pseudo-)random number generator will produce the same "random" numbers in the same order. So:
Random rand = null;
Then, either create a new "seed" and save it to a file:
long seed = System.currentTimeMillis();
rand = new Random(seed);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("whatever.txt"));
writer.write(Long.toString(seed));
writer.close();
} catch(IOException e) {
e.printStackTrace();
}
Or read a previously saved value back in:
long seed = 0;
try {
BufferedReader reader = new BufferedReader(new FileReader("whatever.txt"));
seed = Long.parseLong(reader.readLine());
reader.close();
} catch(IOException e) {
e.printStackTrace();
}
rand = new Random(seed);
Do note that this code doesn't check to make sure the file exits, or that the file isn't empty, or that the file doesn't contain something other than a valid number, etc...
这篇关于如何保存文件和阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!