Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Preparation Code for FILE IO
- -------------------------------------------------------- চেম্পল কুড! ---------------------------------------------------------------
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.FileWriter;
- import java.io.PrintWriter;
- public class g {
- public static void main(String args[]) throws IOException {
- InputStreamReader cin = null;
- FileWriter fr = new FileWriter("INPUT.txt", true); // This is only creating, thats why not surronded with Try-catch
- PrintWriter pin = null;
- try {
- cin = new InputStreamReader(System.in); // System.in , because we gonna input from console, not from any text File
- pin = new PrintWriter(fr,true);
- System.out.println("Enter characters, 'q' to quit.");
- char c;
- do {
- c = (char) cin.read(); // Converting BYTES to CHAR
- System.out.print(c); // Consloe output
- pin.printf("%s", c); // pin = Object of PrintWritter, works as like printf for FILE (text file basically)
- } while(c != 'q');
- }finally {
- if (cin != null) {
- cin.close();
- pin.close();
- }
- }
- }
- }
- -------------------------------------------------------------SUMMATION Ber korar CODE--------------------------------------------
- Note: to run this code, you have to crate text file and put some value there, which will be named "source.txt"
- import java.io.IOException;
- import java.io.*;
- public class File {
- public static void main(String[] args) throws IOException {
- BufferedReader br = null;
- //BufferedWriter bw = null;
- PrintWriter pw = null;
- FileWriter fr = new FileWriter("output.txt");
- try {
- br = new BufferedReader(new FileReader("source.txt")); // Reader means, "Go read from the file
- //"something.txt" this #Buff Buff thing can just only Read! no logical, storing or other to do!
- //bw = new BufferedWriter(new FileWriter("hudai.txt")); // This buff thing can write (can create 'someting.txt' )
- // with the help of FileWritter!
- pw = new PrintWriter(fr); // This is PRINT_WRITER, kind of work as "println" for file FILE related balSal!
- String line;
- int sum = 0;
- while ((line = br.readLine()) != null)
- sum = sum + Integer.parseInt(line);
- // System.out.println(sum);
- pw.println(sum);
- // bw.write(sum);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- br.close();
- pw.close();
- // bw.close();
- }
- }
- -----------------------------------------------FIND MAXIMUM VALUE FROM FILE -------------------------------------------------
- Task : ei code tar output tmi console e na dekhye arekta File e dekhaba!
- import java.io.*;
- import java.util.*;
- import java.io.File;
- class LargeNum {
- public static void main(String[] args) {
- try {
- Scanner file = new Scanner(new File("numbers.txt")); // We can also read a file by Scanner! ;)
- int largest = file.nextInt(); // This line telling tht, by nextInt it sayin go read Integer from file nd put those number into 'largest'
- // thats why we take ' int largest '
- while(file.hasNextInt()) { // until found next line it will put one by one number in ' int number ' variable
- int number = file.nextInt();
- if(number > largest) { // This is normal logic of finding maximum, like we do in real life, umuk ki umuker theke boro, boro hoile
- // largest e rakhbe
- largest = number;
- }
- System.out.println(number);
- }
- file.close();
- System.out.println("The largest number in the file is: " + largest);
- } catch(IOException e) {
- System.out.println(e.getMessage());
- }
- }
- }
- ---------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement