File tree 11 files changed +302
-0
lines changed
src/com/bhuwan/java/io/file
11 files changed +302
-0
lines changed Original file line number Diff line number Diff line change
1
+ one
2
+ two
3
+ three
Original file line number Diff line number Diff line change
1
+ four
2
+ five
3
+ six
Original file line number Diff line number Diff line change
1
+ one
2
+ four
3
+ two
4
+ five
5
+ three
6
+ six
Original file line number Diff line number Diff line change
1
+ B100
2
+ true
3
+ 10.2
4
+ Bhuwan
Original file line number Diff line number Diff line change
1
+ /**
2
+ *
3
+ */
4
+ package com .bhuwan .java .io .file ;
5
+
6
+ import java .io .BufferedReader ;
7
+ import java .io .FileReader ;
8
+ import java .io .IOException ;
9
+
10
+ /**
11
+ * @author bhuwan
12
+ *
13
+ */
14
+ public class BufferedReaderDemo {
15
+
16
+ /**
17
+ * @param args
18
+ * @throws IOException
19
+ */
20
+ public static void main (String [] args ) throws IOException {
21
+ try (BufferedReader br = new BufferedReader (new FileReader ("file_writer_demo.txt" ));) {
22
+ String line = br .readLine ();
23
+ while (line != null ) {
24
+ System .out .println (line );
25
+ line = br .readLine ();
26
+ }
27
+ } catch (Exception e ) {
28
+
29
+ }
30
+ }
31
+
32
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ *
3
+ */
4
+ package com .bhuwan .java .io .file ;
5
+
6
+ import java .io .BufferedWriter ;
7
+ import java .io .FileWriter ;
8
+ import java .io .IOException ;
9
+
10
+ /**
11
+ * @author bhuwan
12
+ *
13
+ */
14
+ public class BufferedWriterDemo {
15
+
16
+ /**
17
+ * @param args
18
+ * @throws IOException
19
+ */
20
+ public static void main (String [] args ) throws IOException {
21
+ try (BufferedWriter bw = new BufferedWriter (new FileWriter ("file_writer_demo.txt" , true ));
22
+ BufferedWriter bw1 = new BufferedWriter (new FileWriter ("file_writer_demo.txt" ));
23
+ // initializing double buffered writer object.
24
+ BufferedWriter bw2 = new BufferedWriter (new BufferedWriter (new FileWriter ("file_writer_demo.txt" )));) {
25
+ bw .write (66 );
26
+ bw .write ("huwan" );
27
+ bw .newLine ();
28
+ bw .write ("Hello BufferWriter!!" );
29
+ bw .newLine ();
30
+ bw .write (new char [] { 'A' , 'B' , 'C' });
31
+ bw .newLine ();
32
+ bw .write ('X' );
33
+ bw .newLine ();
34
+ bw .flush ();
35
+ } catch (Exception e ) {
36
+ System .out .println ("Inside Exception block." );
37
+ }
38
+ }
39
+
40
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ *
3
+ */
4
+ package com .bhuwan .java .io .file ;
5
+
6
+ import java .io .File ;
7
+ import java .io .FileReader ;
8
+ import java .io .IOException ;
9
+
10
+ /**
11
+ * @author bhuwan
12
+ *
13
+ */
14
+ public class FileReaderDemo {
15
+
16
+ /**
17
+ * @param args
18
+ * @throws IOException
19
+ */
20
+ public static void main (String [] args ) throws IOException {
21
+ System .out .println ("----------------------------------------------" );
22
+ System .out .println ("Reading file using read() method." );
23
+ System .out .println ("----------------------------------------------" );
24
+ FileReader fr = new FileReader ("file_writer_demo.txt" );
25
+ int read = fr .read ();
26
+ while (read != -1 ) {
27
+ System .out .print ((char ) read );
28
+ read = fr .read ();
29
+ }
30
+
31
+ // reading file using read(ch) method
32
+ System .out .println ();
33
+ System .out .println ("----------------------------------------------" );
34
+ System .out .println ("Reading file using read(ch) method." );
35
+ System .out .println ("----------------------------------------------" );
36
+ File file = new File ("file_writer_demo.txt" );
37
+ char [] chars = new char [(int ) file .length ()];
38
+ FileReader fileReader = new FileReader (file );
39
+ fileReader .read (chars );
40
+ for (char c : chars ) {
41
+ System .out .print (c );
42
+ }
43
+ fileReader .close ();
44
+ fr .close ();
45
+ }
46
+
47
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ *
3
+ */
4
+ package com .bhuwan .java .io .file ;
5
+
6
+ import java .io .FileWriter ;
7
+ import java .io .IOException ;
8
+
9
+ /**
10
+ * @author bhuwan
11
+ *
12
+ */
13
+ public class FileWriterDemo {
14
+
15
+ /**
16
+ * @param args
17
+ * @throws IOException
18
+ */
19
+ public static void main (String [] args ) throws IOException {
20
+ try (
21
+ // FileWriter fw = new FileWriter("file_writer_demo.txt");
22
+ FileWriter fw = new FileWriter ("file_writer_demo.txt" , true );) {
23
+ fw .write (66 );
24
+ fw .write ("huwan" );
25
+ fw .write ("\n " );
26
+ fw .write ("Hello FileWriter!!" );
27
+ fw .write ("\n " );
28
+ fw .write (new char [] { 'A' , 'B' , 'C' });
29
+ fw .write ("\n " );
30
+ fw .write ('X' );
31
+ fw .write ("\n " );
32
+ fw .flush ();
33
+ } catch (Exception e ) {
34
+ System .out .println ("Inside Exception block." );
35
+ }
36
+ }
37
+
38
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ *
3
+ */
4
+ package com .bhuwan .java .io .file ;
5
+
6
+ import java .io .BufferedReader ;
7
+ import java .io .FileReader ;
8
+ import java .io .PrintWriter ;
9
+
10
+ /**
11
+ * Read data from two files file1.txt and file2.txt and write to file3.txt.
12
+ *
13
+ * @author bhuwan
14
+ *
15
+ */
16
+ public class MergeDataFromTwoFilestoSingleFile {
17
+
18
+ /**
19
+ * @param args
20
+ */
21
+ public static void main (String [] args ) {
22
+ try (PrintWriter pw = new PrintWriter ("file3.txt" ); BufferedReader br = new BufferedReader (new FileReader ("file1.txt" ));
23
+ BufferedReader br2 = new BufferedReader (new FileReader ("file2.txt" ));) {
24
+ // read from file1
25
+ String line = br .readLine ();
26
+ while (line != null ) {
27
+ pw .println (line );
28
+ line = br .readLine ();
29
+ }
30
+
31
+ // read from file2
32
+ line = br2 .readLine ();
33
+ while (line != null ) {
34
+ pw .println (line );
35
+ line = br2 .readLine ();
36
+ }
37
+ pw .flush ();
38
+ } catch (Exception e ) {
39
+ System .out .println ("Inside exception catch block." );
40
+ }
41
+ }
42
+
43
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ *
3
+ */
4
+ package com .bhuwan .java .io .file ;
5
+
6
+ import java .io .BufferedReader ;
7
+ import java .io .FileReader ;
8
+ import java .io .PrintWriter ;
9
+
10
+ /**
11
+ * <pre>
12
+ * Read data from two files file1.txt and file2.txt and write to file3.txt where write to file should be done alternatively.
13
+ * Steps:
14
+ * 1. read first line from file1 and write to file3
15
+ * 2. read first line from file2 and write to file3
16
+ * 3. read second line from file1 and write to file3
17
+ * 4. read second line from file2 and write to file3
18
+ * and so on.........
19
+ * </pre>
20
+ *
21
+ * @author bhuwan
22
+ *
23
+ */
24
+ public class MergeDataFromTwoFilestoSingleFileAlternatively {
25
+
26
+ /**
27
+ * @param args
28
+ */
29
+ public static void main (String [] args ) {
30
+ try (PrintWriter pw = new PrintWriter ("file3.txt" ); BufferedReader br = new BufferedReader (new FileReader ("file1.txt" ));
31
+ BufferedReader br2 = new BufferedReader (new FileReader ("file2.txt" ));) {
32
+
33
+ // read from file1
34
+ String line1 = br .readLine ();
35
+ // read from file2
36
+ String line2 = br2 .readLine ();
37
+ while (line1 != null || line2 != null ) {
38
+ if (line1 != null ) {
39
+ pw .println (line1 );
40
+ line1 = br .readLine ();
41
+ }
42
+ if (line2 != null ) {
43
+ pw .println (line2 );
44
+ line2 = br2 .readLine ();
45
+ }
46
+ }
47
+ pw .flush ();
48
+ } catch (Exception e ) {
49
+ System .out .println ("Inside exception catch block." );
50
+ }
51
+ }
52
+
53
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ *
3
+ */
4
+ package com .bhuwan .java .io .file ;
5
+
6
+ import java .io .PrintWriter ;
7
+
8
+ /**
9
+ * @author bhuwan
10
+ *
11
+ */
12
+ public class PrintWriterDemo {
13
+
14
+ /**
15
+ * @param args
16
+ */
17
+ public static void main (String [] args ) {
18
+ try (PrintWriter pw = new PrintWriter ("file_writer_demo.txt" );) {
19
+ // will write B to the file, since PrintWriter using write method.
20
+ pw .write (66 );
21
+ // will write 100 directly to the file.
22
+ pw .println (100 );
23
+ pw .println (true );
24
+ pw .println (10.2 );
25
+ pw .println ("Bhuwan" );
26
+ pw .flush ();
27
+ } catch (Exception e ) {
28
+ System .out .println ("Inside exception catch block." );
29
+ }
30
+
31
+ }
32
+
33
+ }
You can’t perform that action at this time.
0 commit comments