java写入内容到本地文件 -读取文件内容
/** 日志记录
* @author sys
* @param content 要写入的类容
* @param path 目标路径 c:/log/
* @param filename 文件名 log.txt
*/
public static void writeFileToLoaction(String content,String path,String filename) {
try {
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
File f = new File(path+"//"+filename);
if(!f.exists())
f.createNewFile(); FileWriter fw = new FileWriter(f.getAbsoluteFile(),true);//追加
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content+"\r\n\r\n\r\n");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读文件 text
*/
public static String getStringstrByLocation(String filepath) {
StringBuilder bufferstr=new StringBuilder();
InputStreamReader contentfile = null ;
BufferedReader Inputfile = null ;
try {
File f = new File(filepath);
if (!f.exists()) {
f.createNewFile();
}
contentfile = new InputStreamReader(
new FileInputStream(f), "UTF-8");
Inputfile = new BufferedReader(contentfile);
String filerecord = "";
while ((filerecord=Inputfile.readLine()) != null) {
bufferstr.append(filerecord);
}
} catch (Exception e) {
e.printStackTrace();
return "";
}finally{
try {
Inputfile.close();
contentfile.close();
} catch (IOException e) {
//e.printStackTrace();
}
}
return bufferstr.toString();
} public static void main(String[] args) {
System.out.println(getStringstrByLocation("c:/log/123.txt"));
}
java写入内容到本地文件 -读取文件内容的更多相关文章
- Java如何实现按指定行读取文件
最近在开发实战中,遇到了一个这样的技术情景: 把log4j生成的日志文件定时刷进MySQL数据库,比如三个小时刷一次,那么每次刷数据的时候,如何控制文件读取是从上一次文件读取结束的地方开始继续读取的? ...
- Java使用FileReader(file)、readLine()读取文件,以行为单位,一次读一行,一直读到null时结束,每读一行都显示行号。
//Java使用FileReader(file).readLine()读取文件,以行为单位,一次读一行,一直读到null时结束,每读一行都显示行号. public static void readFi ...
- phpspreadsheet 中文文档(六)读写文件+读取文件
2019年10月11日14:05:58 读写文件 从体系结构您已经知道,使用基本PhpSpreadsheet类无法对持久性存储进行读写.为此,PhpSpreadsheet提供读者和作家,这是实现\Ph ...
- java编程如何实现从本地里读取文件1,写入到本地另一个文件2里(多种场景)
不多说,直接上干货! 有时候,我们需要用到这样的一个场景. ReadLocalFile1WriteLocalFile2.java (以下是相当于复制,读取文件1里的全部内容,并写入到文件2里) pac ...
- php 写入文件 读取文件内容
1.写入文件 fopen("文件名.扩展名","操作方式") fwrite(读取的文件,"写入的文件"); fclose(打开的对象变量); ...
- java 在MySQL中存储文件,读取文件(包括图片,word文档,excel表格,ppt,zip文件等)
转自:https://blog.csdn.net/u014475796/article/details/49893261 在设计到数据库的开发中,难免要将图片或文档文件(如word)插入到数据库中的情 ...
- 用python实现文件读取和内容替换
infile = open("D:/test.txt", "r") #打开文件 outfile = open("D:/pp2.txt", & ...
- 【java】递归统计本地磁盘所有文件,提取重复文件,JDK8 map迭代
package com.sxd.createDao; import java.io.File; import java.time.LocalDateTime; import java.util.Has ...
- android 读取EXcel 文件 读取文件内存卡的权限
android 采用Java的读取xls文件的方式实现. 需要导入第三方Jxl.jar 包. 代码改自 其他博主 : 这只摘录下 读取xls文件的部分代码,当然这个代码在安卓平台需要添加下面的权限 ...
随机推荐
- [LeetCode] 114. Flatten Binary Tree to Linked List 将二叉树展平为链表
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
- [LeetCode] 168. Excel Sheet Column Title 求Excel表列名称
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- SpringBoot读取properties中的属性值
1.在application.properties中添加以下内容: learn.blog.name=hello learn.blog.title=千回教育系统 2.新增属性关联的类: package ...
- Nginx反向代理+负载均衡简单实现(手动申请https证书,申请免费https证书,http强转https)
背景:A服务器(192.168.1.8)作为nginx代理服务器B服务器(192.168.1.150)作为后端真实服务器 现在需要访问https://testwww.huanqiu.com请求时从A服 ...
- [转帖]Socat 入门教程
https://www.hi-linux.com/posts/61543.html 现在安装k8s 必须带 socat 今天看一下socat 到底是啥东西呢. Socat 是 Linux 下的一个多功 ...
- 删除List集合中的元素你碰到过这样的陷阱吗?
删除List的三种方式: (1) 普通遍历 : @Test public void testList(){ ArrayList<String> list = new ArrayList&l ...
- c#动态类转json,再由json转xml
直接上代码了,多说无意了. using System; using System.Collections; using System.Collections.Generic; using System ...
- django.db.utils.ProgrammingError: 1146 解决办法
出现原因: 因为直接在mysql中删除了表或者在执行过一次迁移后,在modles中修改了表名及对应的方法和引用 产生后果: 1.迁移的过程中可能出现表不存在的报错情况 2.迁移过程没有报错,在admi ...
- JMeter中文乱码的解决
对于JMeter中文乱码分为两部分: 1.请求(request):由于引用了csv文件当csv文件编码与JMeter不统一可能造成服务器收到数据的中文乱码 2.返回(response):由于服务器返回 ...
- M-SOLUTIONS Programming Contest
A.(n-2)*180 #include<cstdio> #include<cstring> #include<iostream> #include<algo ...