try {
FileWriter filewriter = new FileWriter(
new File("f:/1.txt"), true);
String content = text_1.getText();
filewriter.write(content);
filewriter.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
InputStream in = Config.class.getResourceAsStream("/config/config.properties");
try {
p.load(in);
} catch (Exception e) {
throw new RuntimeException("error when try read config:"+property);
}
配置文件应该放在/src/config/config.properties
/config/config.properties 第一个/指的应该就是classpath的root, 即源码的src目录
File f2 = new File("E:/99.inc");
InputStreamReader read = null;
try {
read = new InputStreamReader(new FileInputStream(f2),
"UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedReader reader2 = new BufferedReader(read);
String line2;
try {
while ((line2 = reader2.readLine()) != null) {
System.out.println(line2);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
File f = new File("/Users/aaa.txt");
String strContent = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(f));
String line;
while ((line = reader.readLine()) != null) {
strContent += line;
}
System.out.println(strContent);
} catch (FileNotFoundException e2) {
e2.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (reader!=null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
utf-8
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8"));
package com.lich;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
File f = new File("c:\\2222.csv");
BufferedReader reader = null;
try {
int linenum = 0;
InputStream fis = new FileInputStream("c:\\2222.csv");
InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-16"));
reader = new BufferedReader(isr);
String line;
while ((line = reader.readLine()) != null) {
linenum++;
int count = matchCount(line, "222/0");
if(count!=2){
System.out.print(linenum+" "+line.length()+" ");
System.out.print(line);
System.out.println(count);
}
}
} catch (FileNotFoundException e2) {
e2.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (reader!=null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static int matchCount(String str, String findStr){
Pattern p = Pattern.compile(findStr);
Matcher m = p.matcher(str);
int count = 0;
while (m.find()){
count +=1;
}
return count;
}
}
本文介绍Java中处理文件读写的多种方式,包括使用FileWriter写入文本文件、通过BufferedReader读取不同编码的文件内容,并展示了如何利用正则表达式进行文本匹配计数。
829

被折叠的 条评论
为什么被折叠?



