Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Фамилия ; Должность ; e-mail
1;2;1
2;5;2
3;8;3
4 changes: 4 additions & 0 deletions outData.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Фамилиия ; Должность ; e-mail
6;6;6
7;7;7
8;8;8
93 changes: 93 additions & 0 deletions src/ru/gb/Dmitrieva/HomeWorkApp/Lesson_5/AppData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package ru.gb.Dmitrieva.HomeWorkApp.Lesson_5;


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class AppData {

private String[] header;
private int[][] data;
private FileReader file;

public AppData(FileReader fileReader) {
this.file = fileReader;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Интересное решение, + вам в карму

}

public AppData(FileWriter file) {}
Copy link

@evdachev evdachev Jun 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это зачем ? - из кармы


public void setHeadersAndData(String[] headerString) {
this.header = headerString;
this.data = getData();
}

public String[] getHeader() {
return header;
}

public String getHeaderString() {
StringBuilder str = new StringBuilder();
for (int i = 0; i < getHeader().length; i++) {
if (getHeader().length - 1 != i) {
str.append(getHeader()[i]).append(";");
}
else {
str.append(getHeader()[i]).append("\n");
}
}
return str.toString();
}

public int[][] getData() {
return data;
}

public void setData(int[][] data) {
this.data = data;
}

public String separatingGetData() {
StringBuilder temp = new StringBuilder();
for (int[] datum : data) {
for (int j = 0; j < datum.length; j++) {
if (j != data.length - 1)
temp.append(datum[j]).append(";");
else temp.append(datum[j]);
}
temp.append("\n");
}
return temp.toString();
}

public FileReader getFileReader() {
return file;
}

public void readAll() throws IOException {
BufferedReader bufferedReader = new BufferedReader(getFileReader());
ArrayList<String> tempList = new ArrayList<>();
String str;
while ((str = bufferedReader.readLine()) != null) {
tempList.add(str);
}
bufferedReader.close();
getFileReader().close();
String[][] arrStr = new String[tempList.size() - 1][];
for (int i = 0; i < tempList.size(); i++) {
if (i == 0) {
this.header = tempList.get(i).split(";");
} else {
arrStr[i - 1] = tempList.get(i).split(";");
}
}
this.data = new int[arrStr.length][arrStr[0].length];
for (int a = 0; a < arrStr.length; a++) {
for (int b = 0; b < arrStr[a].length; b++) {
this.data[a][b] = Integer.parseInt(arrStr[a][b]);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.gb.Dmitrieva.HomeWorkApp.Lesson_5;


import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

public class CreateReaderWriterCSV {

public static void main(String[] args) throws IOException {
AppData appData = new AppData(new FileWriter("outData.csv"));
appData.setHeadersAndData(new String[]{" Фамилиия ", " Должность ", " e-mail "});
appData.setData(new int[][]{{6, 6, 6}, {7, 7, 7}, {8, 8, 8}});
FileWriter fileWriterInFile = new FileWriter("outData.csv", false);
fileWriterInFile.write(appData.getHeaderString());
fileWriterInFile.write(appData.separatingGetData());
fileWriterInFile.close();

AppData appDataRead = new AppData(new FileReader("data.csv"));
appDataRead.readAll();
System.out.println(Arrays.toString(appDataRead.getHeader()));
System.out.println(Arrays.deepToString(appDataRead.getData()));

}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем тут перенос строки?

}