Skip to content

Instantly share code, notes, and snippets.

@FreeFly19
Created November 21, 2016 20:41
Show Gist options
  • Select an option

  • Save FreeFly19/d019f541500d1674d31dd67aa3f1d392 to your computer and use it in GitHub Desktop.

Select an option

Save FreeFly19/d019f541500d1674d31dd67aa3f1d392 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileApiExample {
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("Some string!".getBytes());
if(!file.delete()) {
System.out.println("Ми не можемо видалити файл, який відкритий (має не закритий I/O stream)");
}
outputStream.close();
if(file.delete()) {
System.out.println("Тепер все ок");
}
}
}
import java.io.FileOutputStream;
import java.io.IOException;
public class InputStreamExample {
public static void main(String[] args) throws IOException {
FileOutputStream outputStream = new FileOutputStream("D:/test-java.txt");
String s = "Hello, world!";
outputStream.write(s.getBytes());
outputStream.close();
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class OutputStreamIntsExample {
public static void main(String[] args) throws IOException {
FileInputStream inputStream = new FileInputStream("D:/test-java.txt");
Scanner scanner = new Scanner(inputStream);
int sum = 0;
while (scanner.hasNextInt()) {
sum += scanner.nextInt();
}
System.out.println(sum);
inputStream.close();
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class OutputStreamStringExample {
public static void main(String[] args) throws IOException {
FileInputStream inputStream = new FileInputStream("D:/test-java.txt");
Scanner scanner = new Scanner(inputStream);
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
inputStream.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment