Facebook
From rafio, 3 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 75
  1. package com.company;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.util.ArrayList;
  6. import java.util.Formatter;
  7. import java.util.Scanner;
  8.  
  9. public class ReadGraph {
  10.     public Graph readOfFile(){
  11.         File fIn = new File("In0205.txt");
  12.         File fInP = new File("In0205P.txt");
  13.         Graph graph = null;
  14.  
  15.         if(!fIn.exists()){
  16.             try{
  17.                 fIn.createNewFile();
  18.             }catch (Exception e){
  19.                 System.out.println(e.getMessage());
  20.             }
  21.         }
  22.         if(!fInP.exists()){
  23.             try{
  24.                 fInP.createNewFile();
  25.             }catch (Exception e){
  26.                 System.out.println(e.getMessage());
  27.             }
  28.         }
  29.  
  30.         if(fIn.canRead()){
  31.             try{
  32.  
  33.                 Scanner scFIn = new Scanner(fIn);
  34.  
  35.                 int licznik = -1;
  36.                 int ilosc = 0;
  37.  
  38.  
  39.                 while(scFIn.hasNextLine()){
  40.                     Pomocniczy(scFIn.nextLine());
  41.  
  42.                     Scanner scFInP = new Scanner(fInP);
  43.                     if(licznik == -1){
  44.                         licznik++;
  45.                         ilosc = scFInP.nextInt();
  46.                         graph = new Graph(ilosc);
  47.                     }
  48.                     else{
  49.                         while (scFInP.hasNext()){
  50.                         graph.addEdge(licznik,(scFInP.nextInt()-1));
  51.                         }
  52.  
  53.                         licznik++;
  54.                     }
  55.  
  56.  
  57.                     scFInP.close();
  58.                 }
  59.  
  60.                 scFIn.close();
  61.             }catch (Exception e){
  62.                 System.out.println(e.getMessage());
  63.             }
  64.         }
  65.         return graph;
  66.     }
  67.  
  68.     protected void Pomocniczy(String linia){
  69.         try{
  70.             File fInP = new File("In0205P.txt");
  71.             FileWriter fw = new FileWriter(fInP, false);
  72.             Formatter formatter = new Formatter(fw);
  73.  
  74.             formatter.format("%s",linia);
  75.  
  76.             formatter.close();  fw.close();
  77.         }catch (Exception e){
  78.             System.out.println(e.getMessage());
  79.         }
  80.     }
  81. }
  82.