Advertisement
Guest User

Untitled

a guest
Sep 17th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. package com.devcolibri.tools;
  2.  
  3. import java.io.*;
  4.  
  5. public class FileWorker {
  6.  
  7. public static void write(String fileName, String textFromFile) {
  8. File file = new File(fileName);
  9.  
  10. try {
  11. if(!file.exists()){
  12. file.createNewFile();
  13. }
  14.  
  15. PrintWriter out = new PrintWriter(file.getAbsoluteFile());
  16.  
  17. try {
  18. out.print(textFromFile);
  19. } finally {
  20. out.close();
  21. }
  22. } catch(IOException e) {
  23. throw new RuntimeException(e);
  24. }
  25. }
  26.  
  27. public static String read(String fileName) throws FileNotFoundException {
  28. StringBuilder sb = new StringBuilder();
  29. File file = new File(fileName);
  30. exists(fileName);
  31.  
  32. try {
  33. BufferedReader in = new BufferedReader(new FileReader( file.getAbsoluteFile()));
  34. try {
  35. String s;
  36. while ((s = in.readLine()) != null) {
  37. sb.append(s);
  38. sb.append("\r\n");
  39. }
  40. } finally {
  41. in.close();
  42. }
  43. } catch(IOException e) {
  44. throw new RuntimeException(e);
  45. }
  46. return sb.toString();
  47. }
  48.  
  49. public static void update(String nameFile, String newText) throws FileNotFoundException {
  50. exists(nameFile);
  51. StringBuilder sb = new StringBuilder();
  52. String oldFile = read(nameFile);
  53. sb.append(oldFile);
  54. sb.append(newText);
  55. write(nameFile, sb.toString());
  56. }
  57.  
  58. public static void delete(String nameFile) throws FileNotFoundException {
  59. exists(nameFile);
  60. new File(nameFile).delete();
  61. }
  62.  
  63. private static void exists(String fileName) throws FileNotFoundException {
  64. File file = new File(fileName);
  65. if (!file.exists()){
  66. throw new FileNotFoundException(file.getName());
  67. }
  68. }
  69.  
  70. }
  71.  
  72.  
  73.  
  74. package com.devcolibri.core;
  75.  
  76. import com.devcolibri.tools.FileWorker;
  77.  
  78. import java.io.FileNotFoundException;
  79.  
  80. /**
  81. * User: devcolibri.com
  82. */
  83. public class WorkInFile {
  84.  
  85. private static String text = "This new text2\nThis new text3\nThis new text4\n";
  86. private static String fileName = "D://Current tasks//STT//meeting_negotiations_corpus_material//info_files//1406901600000.info";
  87. private static String fileNameFinal = "D://Current tasks//STT//meeting_negotiations_corpus_material//info_files//1406901600000.info";
  88.  
  89. public static void main(String[] args) throws FileNotFoundException {
  90.  
  91. //Чтение файл
  92. String textFromFile = FileWorker.read(fileName);
  93. System.out.println(textFromFile);
  94.  
  95.  
  96. //Запись в файл
  97. FileWorker.write(fileNameFinal, textFromFile);
  98.  
  99.  
  100. //Обновление файла
  101. FileWorker.update(fileNameFinal, "This new text");
  102.  
  103. //Попытка прочитать не существующий файл
  104. //FileWorker.read("no_file.txt");
  105.  
  106.  
  107.  
  108.  
  109.  
  110. //Удаление файла
  111. // FileWorker.delete(fileName);
  112. }
  113.  
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement