Skip to content

Instantly share code, notes, and snippets.

@greensma
Created November 27, 2016 18:47
Show Gist options
  • Save greensma/49cc4763edd40c60cc73c0f246a70b32 to your computer and use it in GitHub Desktop.
Save greensma/49cc4763edd40c60cc73c0f246a70b32 to your computer and use it in GitHub Desktop.
HomeWork7
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Reader;
public class Task1 {
public static void main(String[] args) throws IOException {
File file = new File("D:/test-java.txt");
file.createNewFile();
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write("Hello, dear student!\nHow are you?".getBytes());
outputStream.close();
System.out.println(getAllTextFromFile(file));
file.delete();
}
private static void getAllTextFromFile(file){
return;
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
public class Task2 {
public static void main(String[] args) throws IOException {
File file = new File("D:/test-java.txt");
file.createNewFile();
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write("Hello, dear student!\nHow are you?".getBytes());
outputStream.close();
TextFileReader textFileReader = new TextFileReader(file);
System.out.println(textFileReader.getContentAsString());
file.delete();
}
{
try (FileReader reader = new FileReader("D:/test-java.txt")) {
// читаем посимвольно
int c;
while ((c = reader.read()) != -1) {
System.out.print((char) c);
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
{
}
}
private static class TextFileReader {
public boolean getContentAsString() {
}
}
}
import java.util.Scanner;
public class Task3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("First name: ");
String name = in.nextLine();
System.out.print("Last name: ");
String name2 = in.nextLine();
System.out.print("Phone number: ");
int phonenumber = in.nextInt();
System.out.print("Address: ");
String name3 = in.nextLine();
System.out.println("First name: " + name + "Last name: " + name2 + "Phone number: " + phonenumber + "Address: " + name3);
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.time.LocalDate;
public class Human {
public static void main(String[] args) throws CloneNotSupportedException, ClassNotFoundException {
Human h1 = new Human("Vasya", "Bondar", LocalDate.of(15));
Human h2 = new Human("Petro", "Kazyvkin", LocalDate.of(17));
Human h3 = (Human) h1.clone();
System.out.println(h1.toString());
System.out.println(h2.toString());
System.out.println(h3.toString());
try {
FileOutputStream fos = new FileOutputStream("C:\\Java\\human.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
try {
oos.writeObject(h1);
oos.writeObject(h2);
oos.writeObject(h3);
} finally {
oos.close();
}
Human h11, h22, h33;
FileInputStream fis = new FileInputStream("C:\\Java\\human.txt");
ObjectInputStream oin = new ObjectInputStream(fis);
try {
h11 = (Human) oin.readObject();
h22 = (Human) oin.readObject();
h33 = (Human) oin.readObject();
System.out.println(h11.toString());
System.out.println(h22.toString());
System.out.println(h33.toString());
} finally {
oin.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment