Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- class Main{
- public static void main(String args[]){
- int n=0;
- try{
- FileInputStream fis=new FileInputStream("D:\\doe\\code\\java\\File_Handling\\myfile.txt");
- FileOutputStream fos=new FileOutputStream("D:\\doe\\code\\java\\File_Handling\\output.txt");
- Scanner sc=new Scanner(fis); // FOR READING
- PrintWriter pw = new PrintWriter(fos); // FOR WRITING
- while(sc.hasNext()){
- n=sc.nextInt(); // IF YOU WANT STRING JUST USE sc.next()
- if(n%2==0){
- System.out.print("Wrote: ");
- pw.println(n); // WRITE TO FILE ANY DATATYPE IS ACCEPTED
- }
- System.out.println(n);
- }
- fis.close();
- pw.close();
- // fos.close(); // DONT WRITE THIS PW.CLOSE() IS ENOUGH
- } catch(Exception e){
- System.out.println("EXCEPTION");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement