Advertisement
NB52053

:)

Aug 16th, 2017
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. Preparation Code for FILE IO
  2.  
  3. -------------------------------------------------------- চেম্পল কুড! ---------------------------------------------------------------
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.FileWriter;
  7. import java.io.PrintWriter;
  8.  
  9. public class g {
  10.  
  11.     public static void main(String args[]) throws IOException {
  12.         InputStreamReader cin = null;
  13.         FileWriter fr = new FileWriter("INPUT.txt", true); // This is only creating, thats why not surronded with Try-catch
  14.         PrintWriter pin = null;
  15.  
  16.         try {
  17.             cin = new InputStreamReader(System.in);  // System.in , because we gonna input from console, not from any text File
  18.             pin = new PrintWriter(fr,true);
  19.  
  20.             System.out.println("Enter characters, 'q' to quit.");
  21.             char c;
  22.             do {
  23.                 c = (char) cin.read();  // Converting BYTES to CHAR
  24.                 System.out.print(c);   // Consloe output
  25.  
  26.                 pin.printf("%s", c);   // pin = Object of PrintWritter, works as like printf for FILE (text file basically)
  27.  
  28.             } while(c != 'q');
  29.  
  30.  
  31.  
  32.         }finally {
  33.             if (cin != null) {
  34.                 cin.close();
  35.                 pin.close();
  36.             }
  37.         }
  38.     }
  39. }
  40.  
  41. -------------------------------------------------------------SUMMATION Ber korar CODE--------------------------------------------
  42.  
  43. Note: to run this code, you have to crate text file and put some value there, which will be named "source.txt"
  44.  
  45. import java.io.IOException;
  46. import java.io.*;
  47.  
  48.  
  49. public class File {
  50.  
  51.         public static void main(String[] args) throws IOException {
  52.  
  53.             BufferedReader br = null;
  54.             //BufferedWriter bw = null;
  55.             PrintWriter pw =    null;
  56.             FileWriter fr = new FileWriter("output.txt");
  57.  
  58.             try {
  59.                 br = new BufferedReader(new FileReader("source.txt"));  // Reader means, "Go read from the file
  60.                 //"something.txt" this #Buff Buff thing can just only Read! no logical, storing or other to do!
  61.  
  62.                //bw = new BufferedWriter(new FileWriter("hudai.txt")); // This buff thing can write (can create 'someting.txt' )
  63.                 // with the help of FileWritter!
  64.  
  65.                 pw = new PrintWriter(fr);  // This is PRINT_WRITER, kind of work as "println" for file FILE related balSal!
  66.  
  67.  
  68.                 String line;
  69.                 int sum = 0;
  70.                 while ((line = br.readLine()) != null)
  71.                     sum = sum + Integer.parseInt(line);
  72.                // System.out.println(sum);
  73.                 pw.println(sum);
  74.                // bw.write(sum);
  75.  
  76.             }
  77.  
  78.  
  79.             catch (Exception e) {
  80.                 e.printStackTrace();
  81.             }
  82.  
  83.  
  84.             br.close();
  85.             pw.close();
  86.            // bw.close();
  87.  
  88.         }
  89. }
  90.  
  91. -----------------------------------------------FIND MAXIMUM VALUE FROM FILE -------------------------------------------------
  92.  
  93. Task : ei code tar output tmi console e na dekhye arekta File e dekhaba!
  94.  
  95. import java.io.*;
  96. import java.util.*;
  97. import java.io.File;
  98.  
  99. class LargeNum {
  100.  
  101.  
  102.     public static void main(String[] args) {
  103.  
  104.         try {
  105.  
  106.             Scanner file = new Scanner(new File("numbers.txt"));  // We can also read a file by Scanner! ;)
  107.             int largest = file.nextInt();   // This line telling tht, by nextInt it sayin go read Integer from file nd put those number into 'largest'
  108.             // thats why we take ' int largest '
  109.  
  110.             while(file.hasNextInt()) {     // until found next line it will put one by one number in ' int number ' variable
  111.                 int number = file.nextInt();
  112.  
  113.                 if(number > largest) {     // This is normal logic of finding maximum, like we do in real life, umuk ki umuker theke boro, boro hoile
  114.                     // largest e rakhbe
  115.                     largest = number;
  116.                 }
  117.  
  118.               System.out.println(number);
  119.  
  120.             }
  121.  
  122.  
  123.             file.close();
  124.  
  125.  
  126.             System.out.println("The largest number in the file is: " + largest);
  127.  
  128.         } catch(IOException e) {
  129.             System.out.println(e.getMessage());
  130.         }
  131.     }
  132. }
  133.  
  134. ---------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement