Created
November 21, 2016 20:41
-
-
Save FreeFly19/d019f541500d1674d31dd67aa3f1d392 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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("Тепер все ок"); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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