Advertisement
Guest User

Untitled

a guest
May 19th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 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.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8.  
  9.  
  10. public class _09_ListOfProducts {
  11.  
  12.     public static void main(String[] args) {
  13.  
  14.         BufferedReader br = null;
  15.         BufferedWriter bw = null;
  16.         ArrayList<Products> products = new ArrayList<Products>();
  17.  
  18.         try {
  19.  
  20.             String currentLine;
  21.  
  22.             br = new BufferedReader(new FileReader("Input_products.txt"));
  23.  
  24.             while ((currentLine = br.readLine()) != null) {
  25.                
  26.                 String[] splitLine = currentLine.split(" ");
  27.                
  28.                 products.add(new Products(splitLine[0], Double.parseDouble(splitLine[1])));
  29.  
  30.             }
  31.             Collections.sort(products);
  32.            
  33.             bw = new BufferedWriter(new FileWriter("Output_products.txt"));
  34.             for(Products product : products){
  35.                 bw.write(product.getProduct() + " " + (double) product.getPrice() + "\r\n");
  36.             }
  37.            
  38.             bw.close();
  39.         } catch (Exception e) {
  40.             System.out.println("Error!" + e);
  41.         }finally {
  42.             try {
  43.                 if (br != null){
  44.                     br.close();
  45.                 }
  46.             } catch (IOException ex) {
  47.                 ex.printStackTrace();
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement