Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class FileInputStreamDemo
- {
- public static void main(String args[])
- {
- if (args.length != 0)
- {
- System.err.println ("Syntax - FileInputStreamDemo file");
- return;
- }
- try
- {
- // Membuat input stream yang membaca dr file
- InputStream fileInput = new FileInputStream ("mytext.txt");
- int data = fileInput.read(); // Baca byte ke 1
- while (data != -1) // ulangi : hingga end of file (EOF) dicapai
- {
- System.out.write ( data ); // menampilkan byte data ke console
- data = fileInput.read(); // baca byte berikutnya
- }
- fileInput.close(); // Close the file
- }
- catch (IOException ioe)
- {
- System.err.println ("I/O error - " + ioe);
- }
- }}
- ###Copy Lines
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.BufferedReader;
- import java.io.PrintWriter;
- import java.io.IOException;
- public class CopyLines {
- public static void main(String[] args) throws IOException {
- BufferedReader inputStream = null;
- PrintWriter outputStream = null;
- try {
- inputStream = new BufferedReader(new
- FileReader("charStreamsText.txt"));
- outputStream = new PrintWriter(new
- FileWriter("characteroutput.txt"));
- String l;
- int cu = 0;
- while ((l = inputStream.readLine()) != null) {
- outputStream.println(l);
- System.out.println(cu);
- cu += 1;
- }
- } finally {
- if (inputStream != null) {
- inputStream.close();
- }
- if (outputStream != null) {
- outputStream.close();
- }
- }
- }
- }
- ###CopyBytes
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class CopyBytes {
- public static void main(String[] args) throws IOException {
- FileInputStream in = null;
- FileOutputStream out = null;
- try {
- in = new FileInputStream("byteStreamsText.txt");
- out = new FileOutputStream("outagain.txt");
- int c;
- while ((c = in.read()) != -1) {
- out.write(c);
- }
- }
- finally {
- if (in != null) {
- in.close();
- }
- if (out != null) {
- out.close();
- }
- }
- }
- }
- ####CopyCharacters
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class CopyCharacters {
- public static void main(String[] args) throws IOException {
- FileReader inputStream = null;
- FileWriter outputStream = null;
- try {
- inputStream = new FileReader("charStreamsText.txt");
- outputStream = new FileWriter("characteroutput.txt");
- int c;
- int cu = 0;
- while ((c = inputStream.read()) != -1) {
- outputStream.write(c);
- System.out.println(cu);
- cu += 1;
- }
- }
- finally {
- if (inputStream != null) {
- inputStream.close();
- }
- if (outputStream != null) {
- outputStream.close();
- }
- }
- }
- }
- ###ScanSum
- import java.io.FileReader;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.util.Scanner;
- import java.util.Locale;
- public class ScanSum {
- public static void main(String[] args) throws IOException {
- Scanner s = null;
- double sum = 0;
- try {
- s = new Scanner(new BufferedReader(new FileReader("usnumbers.txt")));
- s.useLocale(Locale.US);
- while (s.hasNext()) {
- if (s.hasNextDouble()) {
- sum += s.nextDouble();
- } else {
- s.next();
- }
- }
- } finally {
- s.close();
- }
- System.out.println(sum);
- }
- }
- ####FileOutputStreamDemo
- import java.io.*;
- public class FileOutputStreamDemo
- {
- public static void main(String args[])
- {
- if (args.length != 0)
- {
- System.err.println ("Syntax - FileOutputStreamDemo src dest");
- return;
- }
- String source = "mytext.txt";
- String destination = "myout.txt";
- try
- {
- // Open source file for input
- InputStream input = new FileInputStream(source);
- System.out.println ("Opened " + source + " for reading.");
- OutputStream output = new FileOutputStream (destination); // Ouput output file for output
- PrintStream pout = new PrintStream(output);
- System.out.println ("Opened " + destination + " for writing.");
- int data = input.read();
- while ( data != -1)
- {
- // Write byte of data to our file
- output.write (data);
- // Read next byte
- data=input.read();
- }
- // Close both streams
- pout.println("aku disini");
- input.close();
- output.close();
- System.out.println ("I/O streams closed");
- }
- catch (IOException ioe)
- {
- System.err.println ("I/O error - " + ioe);
- }
- }
- }
Add Comment
Please, Sign In to add comment