Guest User

Untitled

a guest
Jan 18th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5.  
  6. public class TestFile {
  7.  
  8. public static void main(String[] args) throws IOException {
  9. File file = new File("c:\abc.txt");
  10. writeFile(file, "hello");
  11.  
  12. // delete the file
  13. boolean deleted = file.delete();
  14. System.out.println("Deleted? " + deleted);
  15.  
  16. }
  17.  
  18. public static void writeFile(File file, String content) throws IOException {
  19. OutputStream out = null;
  20. try {
  21. out = new FileOutputStream(file);
  22. out.write(content.getBytes("UTF-8"));
  23. } catch (IOException e) {
  24. try {
  25. out.close();
  26. } catch (IOException e1) {
  27. // ignored
  28. }
  29. }
  30. }
  31. }
  32.  
  33. Deleted? false
  34.  
  35. public static void writeFile(File file, String content) throws IOException {
  36. OutputStream out = new FileOutputStream(file);
  37. try {
  38. out.write(content.getBytes("UTF-8"));
  39. } finally {
  40. try {
  41. out.close();
  42. } catch (IOException ignored) {
  43. }
  44. }
  45. }
  46.  
  47. try {
  48. out = new FileOutputStream(file);
  49. out.write(content.getBytes("UTF-8"));
  50. out.close();
  51. } catch (IOException e) {
  52. try {
  53. out.close();
  54. } catch (IOException e1) {
  55. // ignored
  56. }
  57. }
  58.  
  59. public static void main(String[] args) throws IOException {
  60. File file = new File("c:\abc.txt");
  61. writeFile(file, "hello");
  62.  
  63. // delete the file
  64. boolean deleted = file.delete();
  65. System.out.println("Deleted? " + deleted);
  66.  
  67. }
  68.  
  69. public static void writeStringToFile(File file, String data, String encoding) throws IOException {
  70. OutputStream out = new java.io.FileOutputStream(file);
  71. try {
  72. out.write(data.getBytes(encoding));
  73. } finally {
  74. IOUtils.closeQuietly(out);
  75. }
  76. }
  77.  
  78. try (BufferedReader br = new BufferedReader(new FileReader(path)))
  79. {
  80. return br.readLine();
  81. }
Add Comment
Please, Sign In to add comment