Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- public class TestFile {
- public static void main(String[] args) throws IOException {
- File file = new File("c:\abc.txt");
- writeFile(file, "hello");
- // delete the file
- boolean deleted = file.delete();
- System.out.println("Deleted? " + deleted);
- }
- public static void writeFile(File file, String content) throws IOException {
- OutputStream out = null;
- try {
- out = new FileOutputStream(file);
- out.write(content.getBytes("UTF-8"));
- } catch (IOException e) {
- try {
- out.close();
- } catch (IOException e1) {
- // ignored
- }
- }
- }
- }
- Deleted? false
- public static void writeFile(File file, String content) throws IOException {
- OutputStream out = new FileOutputStream(file);
- try {
- out.write(content.getBytes("UTF-8"));
- } finally {
- try {
- out.close();
- } catch (IOException ignored) {
- }
- }
- }
- try {
- out = new FileOutputStream(file);
- out.write(content.getBytes("UTF-8"));
- out.close();
- } catch (IOException e) {
- try {
- out.close();
- } catch (IOException e1) {
- // ignored
- }
- }
- public static void main(String[] args) throws IOException {
- File file = new File("c:\abc.txt");
- writeFile(file, "hello");
- // delete the file
- boolean deleted = file.delete();
- System.out.println("Deleted? " + deleted);
- }
- public static void writeStringToFile(File file, String data, String encoding) throws IOException {
- OutputStream out = new java.io.FileOutputStream(file);
- try {
- out.write(data.getBytes(encoding));
- } finally {
- IOUtils.closeQuietly(out);
- }
- }
- try (BufferedReader br = new BufferedReader(new FileReader(path)))
- {
- return br.readLine();
- }
Add Comment
Please, Sign In to add comment