JAVA读写文件

1 1 /** 2 2 * 3 3 * @Description: 写文件 4 4 * @param @param url 要写到服务器的路径 5 5 * @param @param fileName 要写的文件名 需要加前缀 如 .txt 6 6 * @param @param bodydata 要写的内容 7 7 * @param @return 成功返回1 失败返回0 8 8 * @return String 9 9 */ 1010 public static String writeFile (String url,String fileName,byte[] bodydata){ 1111 StringBuffer sb = new StringBuffer(); 1212 String message = ""; 1313 try{ 1414 sb.append(url).append(fileName); 1515 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(sb.toString(), true)), "UTF-8")); 1616 bw.write(new String(bodydata,"UTF-8")); 1717 message = "1"; 1818 bw.flush(); 1919 bw.close(); 2020 System.out.println("运行完毕!!!"); 2121 }catch(Exception e){ 2222 message = "0"; 2323 e.printStackTrace(); 2424 } 2525 return message; 2626 } 2727 2828 /** 2929 * 3030 * @Description: 读文件 3131 * @param @param filename 文件路径名加文件名 3232 * @param @return 3333 * @return byte[] 3434 */ 3535 public static byte[] readFileByte(String filename){ 3636 BufferedInputStream in = null; 3737 ByteArrayOutputStream bos = null; 3838 try{ 3939 File file = new File(filename); 4040 if(file.isFile() && file.exists()){ //判断文件是否存在 4141 bos = new ByteArrayOutputStream((int)file.length()); 4242 in = new BufferedInputStream(new FileInputStream(file)); 4343 int buf_size = 1024; 4444 byte[] buffer = new byte[buf_size]; 4545 int len = 0; 4646 while(-1 != (len = in.read(buffer,0,buf_size))){ 4747 bos.write(buffer,0,len); 4848 } 4949 }else{ 5050 System.out.println("找不到指定的文件"); 5151 } 5252 in.close(); 5353 bos.close(); 5454 }catch(Exception e){ 5555 e.printStackTrace(); 5656 } 5757 return bos.toByteArray(); 5858 } 5959 6060 /** 6161 * 6262 * @Description: 读文件 6363 * @param @param filepath 要读的文件的路径加文件名称 6464 * @param @param encoding 编码格式 6565 * @param @return 成功返回 1 失败返回 0 6666 * @return String 6767 */ 6868 public static String readFileLine(String filepath,String encoding) { 6969 String message = ""; 7070 try { 7171 File file=new File(filepath); 7272 if(file.isFile() && file.exists()){ //判断文件是否存在 7373 InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式 7474 BufferedReader bufferedReader = new BufferedReader(read); 7575 String lineTxt = null; 7676 while((lineTxt = bufferedReader.readLine()) != null){ 7777 System.out.println(lineTxt); 7878 } 7979 read.close(); 8080 }else{ 8181 System.out.println("找不到指定的文件"); 8282 } 8383 }catch (Exception e) { 8484 System.out.println("读取文件内容出错"); 8585 e.printStackTrace(); 8686 } 8787 return message; 8888 }
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )