Использование CsvReader в ЛР3

This commit is contained in:
Victor 2014-01-06 15:53:42 +02:00
parent ddec6fee2d
commit 68823111ef
2 changed files with 17 additions and 24 deletions

View File

@ -9,7 +9,6 @@ import javax.swing.JButton;
import javax.swing.JDialog; import javax.swing.JDialog;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
import javax.swing.JPanel; import javax.swing.JPanel;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
/** /**
* *

View File

@ -1,13 +1,13 @@
package tse.lr3; package tse.lr3;
import com.annimon.io.CsvReader;
import tse.lr2.*; import tse.lr2.*;
import java.awt.Point; import java.awt.Point;
import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
@ -151,31 +151,25 @@ public class LR_3_Tasks {
} }
private List<Ellipse> readFromCSV(String filename) throws IOException { private List<Ellipse> readFromCSV(String filename) throws IOException {
List<Ellipse> list = new ArrayList<>(); CsvReader<Ellipse> csvReader = new CsvReader<>(new File(filename));
BufferedReader reader = new BufferedReader( csvReader.setReaderHandler(new CsvReader.ReaderHandler<Ellipse>() {
new InputStreamReader( new FileInputStream(filename), "UTF-8" )
); @Override
reader.readLine(); // Класс,X1,Y1,X2,Y2,X3,Y3,X4,Y4 public void onStartRead(File file) { }
String line;
while ( (line = reader.readLine()) != null ) { @Override
Ellipse ellipse = null; public Ellipse createObject(String[] params) {
try { return readFromCsvLine(params);
ellipse = readFromCsvLine(line);
} catch (RuntimeException ex) {
System.out.println(ex.toString());
}
if (ellipse != null) list.add(ellipse);
}
reader.close();
return list;
} }
private Ellipse readFromCsvLine(String line) { @Override
public void onFinishRead(File file) { }
});
return csvReader.readCsvToList(true);
}
private Ellipse readFromCsvLine(String[] params) {
final int POINTS_COUNT = 4; final int POINTS_COUNT = 4;
if (line.isEmpty()) throw new RuntimeException("Пустая строка");
String[] params = line.split(",");
if (params.length < 9) throw new RuntimeException("Неверное количество параметров");
final int[] coords = new int[POINTS_COUNT * 2]; final int[] coords = new int[POINTS_COUNT * 2];
for (int i = 0; i < coords.length; i++) { for (int i = 0; i < coords.length; i++) {
coords[i] = Integer.parseInt(params[i + 1]); coords[i] = Integer.parseInt(params[i + 1]);