How to save and read a file

I am creating a program that puts a random image in a grid layout format. The grid size 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 to save it in a file and read it again so that it displays the same images with the same placement in the grid?

Here is the code 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); } 
0
source share
3 answers

In your rand code of java.util.Random class? If so, you can β€œseed” it yourself and then just save the seed value to a text file. For any given seed, a random number generator (pseudo) will produce the same "random" numbers in the same order. So:

 Random rand = null; 

Then create a new "seed" and save it in 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 the previously saved value:

 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); 

Please note that this code does not check to make sure the file is complete or the file is not empty, or that the file contains nothing but a real number, etc.

+2
source

screaming, reading your question incorrectly.

I would associate each grid value with an array index that would indicate an image / file. When this setting is saved, save a new array with values ​​that are associated with the grid (26 of them must be empty, right?) When the file is opened initially, the program is read from the array, if the array is completely empty, randomized.

- create a foreach loop for grids, and if they are empty, set the array value to null, otherwise set the value of the image associated with it. Use pdraw.checkImage ();

+1
source

Well, you need to save the configuration file with all the necessary information. Perhaps you can use the properties file. This way you are browsing your grid, and every time you find a cell with an image, you save the index and file name. Then, when you reload the program, you look at all potential property values ​​to find those that exist, and then you get the image file name, read the image, and load it into the cell.

+1
source

Source: https://habr.com/ru/post/1305403/


All Articles