lr1 -> task_1 -> e3-e8
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
|||||||
.vscode/*
|
.vscode/*
|
||||||
|
example*
|
||||||
|
libs
|
||||||
34
lr10/task_1/e3.java
Normal file
34
lr10/task_1/e3.java
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package lr10.task_1;
|
||||||
|
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import org.json.simple.JSONArray;
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
|
public class e3 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JSONObject library = new JSONObject();
|
||||||
|
JSONArray books = new JSONArray();
|
||||||
|
|
||||||
|
JSONObject book1 = new JSONObject();
|
||||||
|
book1.put("title", "Война и мир");
|
||||||
|
book1.put("author", "Лев Толстой");
|
||||||
|
book1.put("year", 1869);
|
||||||
|
|
||||||
|
JSONObject book2 = new JSONObject();
|
||||||
|
book2.put("title", "Мастер и Маргарита");
|
||||||
|
book2.put("author", "Михаил Булгаков");
|
||||||
|
book2.put("year", 1967);
|
||||||
|
|
||||||
|
books.add(book1);
|
||||||
|
books.add(book2);
|
||||||
|
|
||||||
|
library.put("books", books);
|
||||||
|
|
||||||
|
try (FileWriter file = new FileWriter("example-json.json")) {
|
||||||
|
file.write(library.toJSONString());
|
||||||
|
System.out.println("Json файл успешно создан!");
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
33
lr10/task_1/e4.java
Normal file
33
lr10/task_1/e4.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package lr10.task_1;
|
||||||
|
|
||||||
|
import java.io.FileReader;
|
||||||
|
import org.json.simple.JSONArray;
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
import org.json.simple.parser.JSONParser;
|
||||||
|
|
||||||
|
public class e4 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
JSONParser parser = new JSONParser();
|
||||||
|
Object obj = parser.parse(new FileReader("example-json.json"));
|
||||||
|
|
||||||
|
JSONObject jsonObject = (JSONObject) obj;
|
||||||
|
|
||||||
|
System.out.println("Корневой элемент: "
|
||||||
|
+ jsonObject.keySet().iterator().next());
|
||||||
|
|
||||||
|
JSONArray jsonArray = (JSONArray) jsonObject.get("books");
|
||||||
|
|
||||||
|
for (Object o : jsonArray) {
|
||||||
|
JSONObject book = (JSONObject) o;
|
||||||
|
System.out.println("\nТекущий элемент: book");
|
||||||
|
System.out.println("Название книги: " + book.get("title"));
|
||||||
|
System.out.println("Автор: " + book.get("author"));
|
||||||
|
System.out.println("Год издания: " + book.get("year"));
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
24
lr10/task_1/e5.java
Normal file
24
lr10/task_1/e5.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package lr10.task_1;
|
||||||
|
|
||||||
|
import org.jsoup.Jsoup;
|
||||||
|
import org.jsoup.nodes.Document;
|
||||||
|
import org.jsoup.nodes.Element;
|
||||||
|
import org.jsoup.select.Elements;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class e5 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String url = "https://itlearn.ru/first-steps";
|
||||||
|
try {
|
||||||
|
Document doc = Jsoup.connect(url).get();
|
||||||
|
|
||||||
|
Elements links = doc.select("a[href]");
|
||||||
|
|
||||||
|
for (Element link : links) {
|
||||||
|
System.out.println(link.attr("abs:href"));
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
46
lr10/task_1/e6.java
Normal file
46
lr10/task_1/e6.java
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package lr10.task_1;
|
||||||
|
|
||||||
|
import org.jsoup.Jsoup;
|
||||||
|
import org.jsoup.nodes.Document;
|
||||||
|
import org.jsoup.nodes.Element;
|
||||||
|
import org.jsoup.nodes.Node;
|
||||||
|
import org.jsoup.select.Elements;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class e6 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
// Получаем HTML-код страницы
|
||||||
|
Document doc = Jsoup.connect("http://fat.urfu.ru/index.html").get();
|
||||||
|
|
||||||
|
// Извлекаем список новостей
|
||||||
|
Elements newsParent = doc
|
||||||
|
.select("body > table > tbody > tr > td > div > table > " +
|
||||||
|
"tbody > tr:nth-child(5) > td:nth-child(3) > table > tbody > " +
|
||||||
|
"tr > td:nth-child(1)");
|
||||||
|
|
||||||
|
for (int i = 3; i < 20; i++) {
|
||||||
|
if (!(i % 2 == 0)) {
|
||||||
|
List<Node> nodes = newsParent.get(0).childNodes();
|
||||||
|
|
||||||
|
System.out.println("Тема : " +
|
||||||
|
((Element) nodes.get(i))
|
||||||
|
.getElementsByClass("blocktitle")
|
||||||
|
.get(0).childNodes().get(0));
|
||||||
|
|
||||||
|
System.out.println("Дата : " +
|
||||||
|
((Element) nodes.get(i))
|
||||||
|
.getElementsByClass("blockdate")
|
||||||
|
.get(0).childNodes().get(0)
|
||||||
|
+
|
||||||
|
"\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// сайт не работает...
|
||||||
42
lr10/task_1/e7.java
Normal file
42
lr10/task_1/e7.java
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package lr10.task_1;
|
||||||
|
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import org.apache.poi.ss.usermodel.*;
|
||||||
|
import org.apache.poi.xssf.usermodel.*;
|
||||||
|
|
||||||
|
public class e7 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
// Создаем новую книгу Excel
|
||||||
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
|
|
||||||
|
// Создаем новый лист в книге
|
||||||
|
XSSFSheet sheet = workbook.createSheet("Товары");
|
||||||
|
|
||||||
|
// Записываем данные в ячейки
|
||||||
|
Row headerRow = sheet.createRow(0);
|
||||||
|
headerRow.createCell(0).setCellValue("Товар");
|
||||||
|
headerRow.createCell(1).setCellValue("Характеристики");
|
||||||
|
headerRow.createCell(2).setCellValue("Стоимость");
|
||||||
|
|
||||||
|
Row dataRow1 = sheet.createRow(1);
|
||||||
|
dataRow1.createCell(0).setCellValue("Книга");
|
||||||
|
dataRow1.createCell(1).setCellValue("Жанр: Фантастика, Автор: Иванов И.И.");
|
||||||
|
dataRow1.createCell(2).setCellValue(500.0);
|
||||||
|
|
||||||
|
Row dataRow2 = sheet.createRow(2);
|
||||||
|
dataRow2.createCell(0).setCellValue("Компьютер");
|
||||||
|
dataRow2.createCell(1).setCellValue("Процессор: Intel Core i5, Оперативная память: 16 Гб");
|
||||||
|
dataRow2.createCell(2).setCellValue(25000.0);
|
||||||
|
|
||||||
|
// Записываем книгу Excel в файл
|
||||||
|
String filePath = "example3.xlsx";
|
||||||
|
try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
|
||||||
|
workbook.write(outputStream);
|
||||||
|
workbook.close();
|
||||||
|
System.out.println("Данные записаны в файл: " + filePath);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Ошибка при записи файла: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
33
lr10/task_1/e8.java
Normal file
33
lr10/task_1/e8.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package lr10.task_1;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import org.apache.poi.ss.usermodel.*;
|
||||||
|
import org.apache.poi.xssf.usermodel.*;
|
||||||
|
|
||||||
|
public class e8 {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
// Открываем файл Excel для чтения
|
||||||
|
String filePath = "example3.xlsx";
|
||||||
|
FileInputStream inputStream = new FileInputStream(filePath);
|
||||||
|
|
||||||
|
// Создаем экземпляр книги Excel из файла
|
||||||
|
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
|
||||||
|
|
||||||
|
// Получаем лист из книги по его имени
|
||||||
|
XSSFSheet sheet = workbook.getSheet("Товары");
|
||||||
|
|
||||||
|
// Перебираем строки и ячейки листа
|
||||||
|
for (Row row : sheet) {
|
||||||
|
for (Cell cell : row) {
|
||||||
|
// Выводим значение ячейки на экран
|
||||||
|
System.out.print(cell.toString() + "\t");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Закрываем файл и освобождаем ресурсы
|
||||||
|
workbook.close();
|
||||||
|
inputStream.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user