Advertisement
SamuilPetrow

Untitled

May 15th, 2014
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.FileWriter;
  6. import java.io.FileReader;
  7. public class ListOfProducts {
  8.  
  9.     public static void main(String[] args) {
  10.         ArrayList<Product> products = new ArrayList<Product>();
  11.         BufferedReader reader;
  12.         BufferedWriter writer = null;
  13.         try {
  14.             reader = new BufferedReader(new FileReader("src/inputProducts.txt"));
  15.             String line = null;
  16.             while ((line = reader.readLine()) != null) {
  17.                String[] splittedLine = line.split(" ");
  18.                products.add(new Product(splittedLine[0], Double.parseDouble(splittedLine[1])));
  19.             }
  20.             Collections.sort(products);
  21.            
  22.             writer = new BufferedWriter(new FileWriter("src/outputProducts.txt"));
  23.             for(Product product : products){
  24.                 writer.write(product.getName() + " " + product.getPrice() + "\r\n");
  25.             }
  26.             writer.close();
  27.         }
  28.         catch (Exception e) {
  29.             System.out.println("Error");
  30.         }
  31.  
  32.     }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement