[Class/Library] FileEditor the easy file Editor by Yekllurt

Discussion in 'Resources' started by Yekllurt, Aug 20, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Yekllurt

    Hi guys,
    this is my new Class/Library with wich you can easy controll your file's write them, delete them, create them , move them, etc.

    Code:java
    1. package me.Yekllurt.Main;
    2.  
    3. import java.io.File;
    4. import java.io.FileNotFoundException;
    5. import java.io.FileWriter;
    6. import java.io.IOException;
    7. import java.nio.file.Files;
    8. import java.nio.file.Path;
    9. import java.nio.file.attribute.BasicFileAttributes;
    10. import java.util.Scanner;
    11.  
    12. /**
    13. * @author Yekllurt
    14. */
    15.  
    16. public class FileEditor {
    17.  
    18. private File file;
    19.  
    20. public FileEditor(File f){
    21. this.file = f;
    22. }
    23.  
    24.  
    25. //Create the file
    26. public void createFile(){
    27. try {
    28. this.file.createNewFile();
    29. } catch (IOException e) {
    30. e.printStackTrace();
    31. }
    32. }
    33.  
    34. //Copy the file
    35. public static void copyFile(Path fileToCopy, Path movePlace){
    36. try {
    37. Files.copy(fileToCopy, movePlace);
    38. } catch (IOException e) {
    39. e.printStackTrace();
    40. }
    41. }
    42.  
    43.  
    44. //Move the file to another place
    45. public static void moveFile(Path fileToCopy, Path movePlace){
    46. try {
    47. Files.move(fileToCopy, movePlace);
    48. } catch (IOException e) {
    49. e.printStackTrace();
    50. }
    51. }
    52.  
    53. //Delete the selected file
    54. public void deleteFile(){
    55. if(file.exists()){
    56. file.delete();
    57. }
    58. }
    59.  
    60.  
    61. //Delete for example all files where end on ,jar
    62. public static void deleteAllFilesEnd(File path, String end){
    63. for(File file : path.listFiles()){
    64. if(file.toString().endsWith(end)){
    65. file.delete();
    66. }
    67. }
    68. }
    69.  
    70.  
    71. //Deletes all files in a directory
    72. public static void deleteAllFiles(File path){
    73. for(File file : path.listFiles()){
    74. file.delete();
    75. }
    76. }
    77.  
    78.  
    79. //Check if in the line contains charackters for example a b c d
    80. public boolean isWriten(int line){
    81. boolean isWriten = false;
    82. int checkLine = 0;
    83. try {
    84. Scanner scanner = new Scanner(this.file);
    85. while(scanner.hasNext()){
    86. checkLine++;
    87. if(line == checkLine){
    88. if(scanner.nextLine() == ""){
    89. isWriten = true;
    90. }else{
    91. isWriten = false;
    92. }
    93. }
    94. }
    95. } catch (FileNotFoundException e) {
    96. e.printStackTrace();
    97. }
    98. return isWriten;
    99. }
    100.  
    101.  
    102. //Write in a line in for example a txt file
    103. public void writeLine(String text, int line){
    104. try {
    105. FileWriter fileWriter = new FileWriter(this.file);
    106. for(int i = 0; i < line; i++){
    107. fileWriter.write("\n");
    108. }
    109. fileWriter.write(text);
    110. fileWriter.close();
    111. } catch (IOException e) {
    112. e.printStackTrace();
    113. }
    114. }
    115.  
    116. //Get the date of creation of the file
    117. public static String getCreationDate(Path path){
    118. String creationDate = "Coudent find the file";
    119. try {
    120. BasicFileAttributes basicFileAttributes = Files.readAttributes(path, BasicFileAttributes.class);
    121. creationDate = basicFileAttributes.creationTime().toString();
    122. } catch (IOException e) {
    123. e.printStackTrace();
    124. }
    125. return creationDate;
    126. }
    127.  
    128.  
    129. //Get the file size in bytes
    130. public static long getFileSize(Path path){
    131. long size = 0;
    132. try {
    133. BasicFileAttributes basicFileAttributes = Files.readAttributes(path, BasicFileAttributes.class);
    134. size = basicFileAttributes.size();
    135. } catch (IOException e) {
    136. e.printStackTrace();
    137. }
    138. return size;
    139. }
    140.  
    141. //Get the last time the file got opend
    142. public static String getLastView(Path path){
    143. String lastView = "Coudent find the file";
    144. try {
    145. BasicFileAttributes basicFileAttributes = Files.readAttributes(path, BasicFileAttributes.class);
    146. lastView = basicFileAttributes.lastAccessTime().toString();
    147. } catch (IOException e) {
    148. e.printStackTrace();
    149. }
    150. return lastView;
    151. }
    152.  
    153.  
    154. //Get the last time the file got modified
    155. public static String getLastModifieTime(Path path){
    156. String lastAcces = "Coudent find the file";
    157. try {
    158. BasicFileAttributes basicFileAttributes = Files.readAttributes(path, BasicFileAttributes.class);
    159. lastAcces = basicFileAttributes.lastModifiedTime().toString();
    160. } catch (IOException e) {
    161. e.printStackTrace();
    162. }
    163. return lastAcces;
    164. }
    165.  
    166. public String getLine(int line){
    167. int checkLine = 0;
    168. String result = "Line " + line + " doesn't exists";
    169. try {
    170. Scanner scanner = new Scanner(this.file);
    171. while(scanner.hasNext()){
    172. checkLine++;
    173. if(line == checkLine){
    174. result = scanner.nextLine();
    175. }
    176. }
    177. } catch (FileNotFoundException e) {
    178. e.printStackTrace();
    179. }
    180. return result;
    181. }
    182.  
    183. }
    184.  
     
  2. Offline

    xTrollxDudex

    Basically a big wrapper over internal library...
     
    Cirno, Phasesaber and _Filip like this.
  3. Offline

    DJSkepter

    Phasesaber likes this.
  4. Offline

    Yekllurt

    It shoud be a library :)
     
  5. Offline

    PandazNWafflez

    Yekllurt

    Stuff like

    Code:
    deleteAllFilesEnd([URL='http://www.google.com/search?hl=en&q=allinurl%3Afile+java.sun.com&btnI=I%27m%20Feeling%20Lucky']File[/URL], [URL='http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky']String[/URL])
    Code:
    moveFile(Path, Path)
    Code:
    copyFile(Path, Path)
    Should just be static because they don't need anything contained in the object. By making them non-static you're just forcing people to create objects unnecessarily if those methods are all they need.
     
  6. Offline

    CeramicTitan Retired Staff

    It should all be static and file name parameters should be parsed in.
     
  7. Offline

    PandazNWafflez

    CeramicTitan Not necessarily - there's no problem with having objects for some of those methods.

    You can do new FileWriter() and use the methods which don't take in Path parameters - in fact that's better practice, but for stuff where the instance file variable isn't used those methods should be static.
     
Thread Status:
Not open for further replies.

Share This Page