13 ก.ค. 2554

JRider

การทำงานเกี่ยวกับไฟล์ด้วย JAVA

File JRider.jar
http://www.4shared.com/file/p-G-8o4K/JRider.html

public class Find {
package rider.file;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Writer;
public class Riderparser {
 private String dirtemp = "";
 public String FiletoString(String path,String encode) {
  StringBuffer strLine = new StringBuffer();
   try {
    BufferedReader in;
    if(!encode.trim().equals(""))
      in = new BufferedReader(new InputStreamReader(new FileInputStream(path),encode));
    else
      in = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
    String str;
    while ((str = in.readLine()) != null) {
     strLine.append(str+"\n");
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  String tmp = strLine.toString();
  byte b[] = tmp.getBytes();
  ByteArrayInputStream in = new ByteArrayInputStream(b);
  for (int i=0; i<2; i++) {
   int c;
   while ((c = in.read()) != -1) {
   if (i == 0) {
    System.out.print((char) c);
   } else {
    System.out.print(Character.toUpperCase((char) c));
    }
   }
   System.out.println();
   in.reset(); 
  }
  return tmp;
 }
 public void Writetxtfile(String filename,String str){
  Writer writer = null;
  File file;
  File dir = new File("C:/Riderfile");
  if(filename.lastIndexOf(".txt")<0)
   filename = filename+".txt";
  if(this.dirtemp.equals("")){
   boolean exits = dir.exists();
   if(!exits){
    dir.mkdir();
    this.dirtemp = "C:/Riderfile";
   }
  }
  filename = this.dirtemp+"/"+filename;
        try {
            file = new File(filename);
            writer = new BufferedWriter(new FileWriter(file));
            writer.write(str);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                    System.out.println("Write file "+filename+" complete!!");
                    //JOptionPane.showMessageDialog(null, "COM"+file.getAbsolutePath());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 }
 public void Copyfile(String srFile,String dtFile) {
  try{
    File f1 = new File(srFile);
    String pathSr = srFile.substring(0,srFile.lastIndexOf(File.separator)+1);
    String pathDt = dtFile.substring(0,srFile.lastIndexOf(File.separator)+1);
    if(pathSr.equals(pathDt)){
     String fullname = srFile.substring(srFile.lastIndexOf(File.separator)+1,srFile.lastIndexOf("."));
     String type = srFile.substring(srFile.lastIndexOf("."),srFile.length());
     File folder = new File(srFile.substring(0,srFile.lastIndexOf(File.separator)));
     File[] listOfFiles = folder.listFiles();
     String temp = pathSr+fullname +" Copy of(0)"+type;
     for (int i = 0; i < listOfFiles.length; i++) {
      if(listOfFiles[i].isFile()){
       String filename = listOfFiles[i].toString();
       if(filename.equals(dtFile)){
        int copy = Integer.parseInt(temp.substring(temp.indexOf("(")+1, temp.indexOf(")")))+1;
        dtFile = pathSr+fullname +" Copy of("+copy+")"+type;
       }
      }
     }
    }
    File f2 = new File(dtFile);
    InputStream in = new FileInputStream(f1);
    OutputStream out = new FileOutputStream(f2);

    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0){
     out.write(buf, 0, len);
    }
     in.close();
     out.close();
     System.out.println("File copied to "+dtFile);
    }
    catch(FileNotFoundException ex){
     System.out.println(ex.getMessage() + " in the specified directory.");
     System.exit(0);
    }
    catch(IOException e){
     System.out.println(e.getMessage());  
    }
 }
 public String Readbyte(String filename) throws IOException {
  File file = new File(filename);
     InputStream is = new FileInputStream(file);
     // Get the size of the file
     long length = file.length();
     // Create the byte array to hold the data
     byte[] bytes = new byte[(int)length];
     // Read in the bytes
     int offset = 0;
     int numRead = 0;
     while (offset < bytes.length
            && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
         offset += numRead;
     }
     // Ensure all the bytes have been read in
     if (offset < bytes.length) {
         throw new IOException("Could not completely read file "+file.getName());
     }
     // Close the input stream and return bytes
     is.close();
     String str= "";
     try {
      str =  getHexString(bytes);
  } catch (Exception e) {
   System.out.println(e.getMessage());
   e.printStackTrace();
  }
  return str;
 }
 
 private String getHexString(byte[] b) throws Exception {
   StringBuffer buf = new StringBuffer();
   for (int i=0; i < b.length; i++) {
    buf.append(
           Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 )+"\n");
   }
   return buf.toString();
  }
 public void MakeDirTemp(){
  String time = String.valueOf(System.currentTimeMillis());
  File file = new File("C:/Riderfile/"+time);
  boolean exits = file.exists();
  if(!exits)
   file.mkdir();
  this.dirtemp = file.toString();
 }
}

}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น