Jump to content

User:WikiGuiGuy/sandbox

From Wikipedia, the free encyclopedia

<source lang="Java"> import java.io.*; import java.nio.file.*; import java.util.ArrayList;

/*

Current methods:
Writer()
writeToFile(String toWrite)
loopWriteToFile(ArrayList<String>)
writeFile(String toWrite)
readFile()
readLength()
intrepretMessage(int message)
createFile()
deleteFile()
moveFile(String destination)
fileName()
setFile(String file)
fileExists()
isWriteable()
suppress()
depress()
  • /

public class WriterFile {

   @SuppressWarnings("CallToPrintStackTrace")
   private String fileName;
   private boolean suppressed;
   private int lines;
   Path path = null;
   public void Writer(String file)
   {
       path = Paths.get(file);
       fileName = file;
   }
   @SuppressWarnings({"ConvertToTryWithResources", "null"})
   public void writeToFile(String toWrite)
   {
       File f = new File(FileName());
       BufferedWriter bw = null;
       FileOutputStream fos = null;
       try {
           fos = new FileOutputStream(f);
           bw = new BufferedWriter(new OutputStreamWriter(fos));
           bw.write(toWrite);
           bw.newLine();
       } catch (FileNotFoundException ex) {
           if(!suppressed)
               ex.printStackTrace();
       } catch (IOException ex) {
           if(!suppressed)
               ex.printStackTrace();
       }
       finally{
           try {
               bw.close();
               fos.close();
           } catch (IOException ex) {
               if(!suppressed)
                   ex.printStackTrace();
           }
       }
   }
   @SuppressWarnings("null")
   public void loopWriteToFile(ArrayList<String> writes){
       File f = new File(FileName());
       BufferedWriter bw = null;
       FileOutputStream fos = null;
       try {
           fos = new FileOutputStream(f);
           bw = new BufferedWriter(new OutputStreamWriter(fos));
           for(String s:writes){
               bw.write(s);
               bw.newLine();
           }
       } catch (FileNotFoundException ex) {
           if(!suppressed)
               ex.printStackTrace();
       } catch (IOException ex) {
           if(!suppressed)
               ex.printStackTrace();
       }
       finally{
           try {
               bw.close();
               fos.close();
           } catch (IOException ex) {
               if(!suppressed)
                   ex.printStackTrace();
           }
       }
   }
   //Problems here
   public void writeFile(String toWrite)
   {
       int i = readLength();
       String s = readFile();
       for(int a = 0;a!=i;a++)
           s += "\n";
       s += toWrite;
       writeToFile(s);
   }
   @SuppressWarnings("UnusedAssignment")
   public String readFile()
   {
       FileReader reader = null;
       BufferedReader bReader = null;
       String allText = "";
       try {
           reader = new FileReader(FileName());
           bReader = new BufferedReader(reader);
           String line;
           while((line = bReader.readLine())!= null)
           {
               allText += line + "\n";
           }
           return allText;
       } catch (FileNotFoundException ex) {
           if(!suppressed)
               ex.printStackTrace();
       } catch (IOException ex) {
           if(!suppressed)
               ex.printStackTrace();
       }
       finally{
           if(reader != null)
               try {
                   reader.close();
           } catch (IOException ex) {
               if(!suppressed)
                   ex.printStackTrace();
           }
           if(bReader != null)
               try {
                   bReader.close();
           } catch (IOException ex) {
               if(!suppressed)
                   ex.printStackTrace();
           }
       }
       return "";
   }
   @SuppressWarnings("UnusedAssignment")
   public int readLength(){
       FileReader reader = null;
       BufferedReader bReader = null;
       String allText = "";
       try {
           reader = new FileReader(FileName());
           bReader = new BufferedReader(reader);
           String line;
           while((line = bReader.readLine())!= null)
           {
               allText += line + "\n";
               lines++;
           }
           return lines;
       } catch (FileNotFoundException ex) {
           if(!suppressed)
               ex.printStackTrace();
       } catch (IOException ex) {
           if(!suppressed)
               ex.printStackTrace();
       }
       finally{
           if(reader != null)
               try {
                   reader.close();
           } catch (IOException ex) {
               if(!suppressed)
                   ex.printStackTrace();
           }
           if(bReader != null)
               try {
                   bReader.close();
           } catch (IOException ex) {
               if(!suppressed)
                   ex.printStackTrace();
           }
       }
       return 0;
   }
   public int createFile()
   {
       int i = -1;
       try{
           Files.createFile(path);
           i = 0;
       } catch (FileAlreadyExistsException e){
           if(!suppressed)
               System.out.println("File already exists.");
           i = 1;
       } catch (IOException ex) {
           i = -1;
       }
       return i;
   }
   public String interpretMessage(int message){
       if(message==0)
           return "Successfull";
       else
           if(message==1)
               return "File already exists";
           else
               if(message==-1)
                   return "ERROR";
               else
                   return "";
   }
   @Deprecated
   public void createFile(Path p)
   {
       if(!Files.exists(path))
       {
           try{
               Files.createFile(p);
           } catch (IOException e){
               if(!suppressed)
                   e.printStackTrace();
           }
       }
   }
   public void deleteFile()
   {
       try{
           Files.deleteIfExists(path);
       } catch (IOException e){
           if(!suppressed)
               e.printStackTrace();
       }
   }
   public void moveFile(String destination)
   {
       try{
           Files.move(path, Paths.get(destination));
       } catch (IOException e){
           if(!suppressed)
               e.printStackTrace();
       }
   }
   public String FileName()
   {
       return fileName;
   }
   public void setFile(String s)
   {
       fileName = s;
       path = Paths.get(s);
   }
   @Deprecated
   public boolean fileIsExists()
   {
       boolean b;
       try{
           createFile();
           b = false;
       }
       catch(Exception e){
           b = true;
       }
       finally{
           try {
               Files.deleteIfExists(path);
           } catch (IOException ex) {
               if(!suppressed){
                   System.out.println('?');
                   ex.printStackTrace();
               }
           }
       }
       return b;
   }
   public boolean fileExists(){
       return Files.exists(path);
   }
   @Deprecated
   public boolean fileExists(Path p){
       return Files.exists(p);
   }
   @Deprecated
   public boolean fileExists(String s){
       File f = new File(s);
       return f.exists();
   }
   @Deprecated
   public boolean fileExists(File f){
       return f.exists();
   }
   public boolean isWriteable(){
       return Files.isWritable(path);
   }
   public void suppress(){
       suppressed = true;
   }
   public void depress(){
       suppressed = false;
   }

}