1 /** 2 * 3 * @Description: write file 4 * @param @param url The path to be written to the server 5 * @param @param fileName The file name to be written needs to be prefixed such as .txt 6 * @param @param bodydata The content to be written 7 * @param @return returns 1 on success, 0 on failure 8 * @return String 9 */ 10 public static String writeFile (String url,String fileName, byte [] bodydata){ 11 StringBuffer sb =new StringBuffer(); 12 String message = ""; 13 try{ 14 sb.append(url).append(fileName); 15 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(sb.toString(), true)), "UTF-8")); 16 bw.write(new String(bodydata,"UTF-8")); 17 message = "1"; 18 bw.flush(); 19 bw.close(); 20 System.out.println("Running!!!" ); 21 } catch (Exception e){ 22 message = "0" ; 23 e.printStackTrace(); 24 } 25 return message; 26 } 27 28 /* * 29 * 30 * @Description: read file 31 * @param @param filename file path name plus file name 32 * @param @return 33 * @return byte[] 34 */ 35 public static byte[] readFileByte(String filename){ 36 BufferedInputStream in = null; 37 ByteArrayOutputStream bos = null; 38 try{ 39 File file = new File(filename); 40 if(file.isFile() && file.exists()){ //判断文件是否存在 41 bos = new ByteArrayOutputStream((int)file.length()); 42 in = new BufferedInputStream(new FileInputStream(file)); 43 int buf_size = 1024; 44 byte[] buffer = new byte[buf_size]; 45 int len = 0; 46 while(-1 != (len = in.read(buffer,0,buf_size))){ 47 bos.write(buffer,0,len); 48 } 49 }else{ 50 System.out.println("找不到指定的文件"); 51 } 52 in.close(); 53 bos.close(); 54 } catch (Exception e){ 55 e.printStackTrace(); 56 } 57 return bos.toByteArray(); 58 } 59 60 /** 61 * 62 * @Description: read file 63 * @param @param filepath to read The path of the file plus the file name 64 * @param @param encoding Encoding format 65 * @param @return Returns 1 for success, returns 0 for failure 66 * @return String 67 */ 68 public static String readFileLine(String filepath,String encoding) { 69 String message = ""; 70 try { 71 File file=new File(filepath); 72 if(file.isFile() && file.exists()){ //判断文件是否存在 73 InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式 74 BufferedReader bufferedReader = new BufferedReader(read); 75 String lineTxt = null; 76 while ((lineTxt = bufferedReader.readLine()) != null ){ 77 System.out.println(lineTxt); 78 } 79 read.close(); 80 } else { 81 System.out.println("Find The specified file cannot be found" ); 82 } 83 } catch (Exception e) { 84 System.out.println("Error reading file content" ); 85 e.printStackTrace(); 86 } 87 return message; 88 }