how to insewtin file using java

Solutions on MaxInterview for how to insewtin file using java by the best coders in the world

showing results for - "how to insewtin file using java"
Rosalie
18 Sep 2020
1
2package com.journaldev.files;
3
4import java.io.BufferedWriter;
5import java.io.File;
6import java.io.FileOutputStream;
7import java.io.FileWriter;
8import java.io.IOException;
9import java.io.OutputStream;
10import java.io.PrintWriter;
11
12public class JavaAppendToFile {
13
14	/**
15	 * Java append to file example
16	 * 
17	 * @param args
18	 */
19	public static void main(String[] args) {
20		String filePath = "/Users/pankaj/Downloads/append.txt";
21
22		String appendText = "This String will be appended to the file, Byte=0x0A 0xFF";
23
24		appendUsingFileWriter(filePath, appendText);
25
26		appendUsingBufferedWriter(filePath, appendText, 2);
27
28		appendUsingPrintWriter(filePath, appendText);
29
30		appendUsingFileOutputStream(filePath, appendText);
31	}
32
33	private static void appendUsingPrintWriter(String filePath, String text) {
34		File file = new File(filePath);
35		FileWriter fr = null;
36		BufferedWriter br = null;
37		PrintWriter pr = null;
38		try {
39			// to append to file, you need to initialize FileWriter using below constructor
40			fr = new FileWriter(file, true);
41			br = new BufferedWriter(fr);
42			pr = new PrintWriter(br);
43			pr.println(text);
44		} catch (IOException e) {
45			e.printStackTrace();
46		} finally {
47			try {
48				pr.close();
49				br.close();
50				fr.close();
51			} catch (IOException e) {
52				e.printStackTrace();
53			}
54		}
55	}
56
57	/**
58	 * Use Stream for java append to file when you are dealing with raw data, binary
59	 * data
60	 * 
61	 * @param data
62	 */
63	private static void appendUsingFileOutputStream(String fileName, String data) {
64		OutputStream os = null;
65		try {
66			// below true flag tells OutputStream to append
67			os = new FileOutputStream(new File(fileName), true);
68			os.write(data.getBytes(), 0, data.length());
69		} catch (IOException e) {
70			e.printStackTrace();
71		} finally {
72			try {
73				os.close();
74			} catch (IOException e) {
75				e.printStackTrace();
76			}
77		}
78	}
79
80	/**
81	 * Use BufferedWriter when number of write operations are more
82	 * 
83	 * @param filePath
84	 * @param text
85	 * @param noOfLines
86	 */
87	private static void appendUsingBufferedWriter(String filePath, String text, int noOfLines) {
88		File file = new File(filePath);
89		FileWriter fr = null;
90		BufferedWriter br = null;
91		try {
92			// to append to file, you need to initialize FileWriter using below constructor
93			fr = new FileWriter(file, true);
94			br = new BufferedWriter(fr);
95			for (int i = 0; i < noOfLines; i++) {
96				br.newLine();
97				// you can use write or append method
98				br.write(text);
99			}
100
101		} catch (IOException e) {
102			e.printStackTrace();
103		} finally {
104			try {
105				br.close();
106				fr.close();
107			} catch (IOException e) {
108				e.printStackTrace();
109			}
110		}
111	}
112
113	/**
114	 * Use FileWriter when number of write operations are less
115	 * 
116	 * @param filePath
117	 * @param text
118	 * @param noOfLines
119	 */
120	private static void appendUsingFileWriter(String filePath, String text) {
121		File file = new File(filePath);
122		FileWriter fr = null;
123		try {
124			// Below constructor argument decides whether to append or override
125			fr = new FileWriter(file, true);
126			fr.write(text);
127
128		} catch (IOException e) {
129			e.printStackTrace();
130		} finally {
131			try {
132				fr.close();
133			} catch (IOException e) {
134				e.printStackTrace();
135			}
136		}
137	}
138
139}
140
similar questions
queries leading to this page
how to insewtin file using java