Advertisement
gitman3

Input from file1 and write to file2 if even

Jun 15th, 2023
1,162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.*;
  3.  
  4. class Main{
  5.     public static void main(String args[]){
  6.        
  7.         int n=0;
  8.         try{
  9.            
  10.             FileInputStream fis=new FileInputStream("D:\\doe\\code\\java\\File_Handling\\myfile.txt");
  11.             FileOutputStream fos=new FileOutputStream("D:\\doe\\code\\java\\File_Handling\\output.txt");
  12.             Scanner sc=new Scanner(fis);                // FOR READING
  13.             PrintWriter pw = new PrintWriter(fos);      // FOR WRITING
  14.            
  15.             while(sc.hasNext()){
  16.                 n=sc.nextInt();                         // IF YOU WANT STRING JUST USE sc.next()
  17.                 if(n%2==0){
  18.                     System.out.print("Wrote: ");
  19.                     pw.println(n);                      // WRITE TO FILE ANY DATATYPE IS ACCEPTED
  20.                 }
  21.                 System.out.println(n);
  22.             }
  23.            
  24.             fis.close();
  25.             pw.close();
  26.             // fos.close();                             // DONT WRITE THIS PW.CLOSE() IS ENOUGH
  27.        
  28.         } catch(Exception e){
  29.             System.out.println("EXCEPTION");
  30.         }
  31.        
  32.        
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement