JAVA读写文件

 1      /**
 2      * 
 3      * @Description: 写文件
 4      * @param @param url 要写到服务器的路径
 5      * @param @param fileName 要写的文件名 需要加前缀 如 .txt
 6      * @param @param bodydata 要写的内容 
 7      * @param @return 成功返回1 失败返回0
 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("运行完毕!!!");
21         }catch(Exception e){
22             message = "0";
23             e.printStackTrace();
24         }
25         return message;
26     }
27 
28       /**
29      * 
30      * @Description: 读文件
31      * @param @param filename 文件路径名加文件名
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: 读文件
63      * @param @param filepath 要读的文件的路径加文件名称
64      * @param @param encoding 编码格式
65      * @param @return 成功返回 1 失败返回 0
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("找不到指定的文件");
82             }
83          }catch (Exception e) {
84              System.out.println("读取文件内容出错");
85              e.printStackTrace();
86          }
87         return message;
88     } 

猜你喜欢

转载自www.cnblogs.com/qinxu/p/8989716.html