txt 文件在服务器上的 读取及写入

本文介绍了一种在Java中高效处理大量数据的方法,通过使用FileWriter和BufferedReader进行文件的写入和读取,特别适用于数据库无法承载的大规模报文参数存储场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

做的外围数据对接,报文参数较多,数据库存不下,只好用txt了, 自己 备用


public class MyFileUtils {
	
	/**
	 * @param txt  写入的内容   
	 * @param channel   来源
	 * @param name     姓名
	 * @param path   路径
	 * /opt/tomcat-finance/txt/  qq ljp
	 * @throws IOException
	 */
	public static void writer(String txt,String channel,String name,String path) throws IOException {
		//		path-->/opt/tomcat-finance/txt/
		FileWriter fw=null;
		BufferedWriter bw=null;
		File file1 = new File(path+channel+name+".txt");
		if(file1.exists()) {
			file1.deleteOnExit();
		}
        try {
            fw = new FileWriter(file1);
            bw = new BufferedWriter(fw);
            bw.write(txt);
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            bw.close();
            fw.close();
		}
	}
	
	/**
	 * 文件内容读取
	 * @param name(需要加后缀)
	 * @param path
	 * @return 文本
	 */
	public static String read(String name,String path){
		//		path-->/opt/tomcat-finance/txt/
        File file = new File(path+name);
        if(!file.exists()) {
        	return null;
        }
        String readData=null;
        StringBuffer reString=new StringBuffer() ;
        try {
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(file), "UTF-8") );
        //    BufferedReader br = new BufferedReader(fr);
            try{
            	while((readData = br.readLine())!=null){
                	reString.append(readData);
                }
            }catch (Exception e){
                    e.printStackTrace();
            }finally {
            	br.close();
            	fr.close();
			}
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
		return reString.toString();
	}
	
	public static void main(String[] args) {
		String read = MyFileUtils.read("qqe549c8dc64e9ca680164e9d87b670069.txt","C:\\Users\\Administrator\\Downloads\\");
		System.out.println(read);
	}
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值