一、txt文件读写:
字节流写入文件:File、FileOutputStream、OutputStreamWriter、BufferedWirter
字符流写入文件:File、FileWirter
字节流读取文件:File、FileInputStream、InputStreamReader、BufferedReader
字符流读取文件:File、FileReader、BufferedReader
1.将数据写入文件:
//字节流写入文件
File file = new File("D:\\new_selfcard\\cardfile\\yangli1025-21-01-20100716-110757.txt");
fos = new FileOutputStream(file);//覆盖原文件
osw = new OutputStreamWriter(fos);
bw = new BufferedWriter(osw);
bw.write(str); //str是指需要写入的内容
bw.newLine();
//字符流写入文件
public static void writeTxtFile2(String fileName,String str){
FileWriter fw = null;
try{
File f = new File(fileName);
if(f.exists()){
f.mkdirs();
}
fw = new FileWriter(f);
fw.write(str);
}catch(IOException e){
e.printStackTrace();
}finally{
try{
fw.close();
}catch(IOException e2){
e2.printStackTrace();
}
}
}
2.从文件中读取数据:
//读取字符文件
public static void readTxtFile(String fileName){
FileReader fr = null;
BufferedReader br = null;
String str = "";
try{
File f = new File(fileName);
fr = new FileReader(f);
br = new BufferedReader(fr);
String tmp;
while((tmp = in.readLine())!=null){ //或者while(br.read()!=-1)
str = br.readLine();
System.out.println(str);
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
fr.close();
br.close();
}catch(IOException e2){
e2.printStackTrace();
}
}
}
//读取字节流文件
public static void readTxtFile(String fileName){
FileInputStream in = null;
InputStreamReader inr = null;
BufferedReader br = null;
String str = "";
try{
File f = new File(fileName);
in = new FileInputStream(f);
inr = new InputStreamReader(in);
br = new BufferedReader(inr);
while(br.read()!=-1){
str = br.readLine();
System.out.println(str);
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
in.close();
inr.close();
br.close();
}catch(IOException e2){
e2.printStackTrace();
}
}
}
public static void main(String[] args) {
PubFunction p = new PubFunction();
p.readTxtFile("D:\\new_selfcard\\cardfile\\yangli1025-21-01-20100716-110757.txt");
}
二、得到目录下文件数及文件名
代码如下:
File f = new File(bxtkPath); //bxtkPath是目录
if(f.exists()){
String[] list = f.list();
int j = list.length; //文件个数
for(int i=0;i<list.length;i++){
pdfname = list[i]; //文件名称
}
}
字节流写入文件:File、FileOutputStream、OutputStreamWriter、BufferedWirter
字符流写入文件:File、FileWirter
字节流读取文件:File、FileInputStream、InputStreamReader、BufferedReader
字符流读取文件:File、FileReader、BufferedReader
1.将数据写入文件:
//字节流写入文件
File file = new File("D:\\new_selfcard\\cardfile\\yangli1025-21-01-20100716-110757.txt");
fos = new FileOutputStream(file);//覆盖原文件
osw = new OutputStreamWriter(fos);
bw = new BufferedWriter(osw);
bw.write(str); //str是指需要写入的内容
bw.newLine();
//字符流写入文件
public static void writeTxtFile2(String fileName,String str){
FileWriter fw = null;
try{
File f = new File(fileName);
if(f.exists()){
f.mkdirs();
}
fw = new FileWriter(f);
fw.write(str);
}catch(IOException e){
e.printStackTrace();
}finally{
try{
fw.close();
}catch(IOException e2){
e2.printStackTrace();
}
}
}
2.从文件中读取数据:
//读取字符文件
public static void readTxtFile(String fileName){
FileReader fr = null;
BufferedReader br = null;
String str = "";
try{
File f = new File(fileName);
fr = new FileReader(f);
br = new BufferedReader(fr);
String tmp;
while((tmp = in.readLine())!=null){ //或者while(br.read()!=-1)
str = br.readLine();
System.out.println(str);
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
fr.close();
br.close();
}catch(IOException e2){
e2.printStackTrace();
}
}
}
//读取字节流文件
public static void readTxtFile(String fileName){
FileInputStream in = null;
InputStreamReader inr = null;
BufferedReader br = null;
String str = "";
try{
File f = new File(fileName);
in = new FileInputStream(f);
inr = new InputStreamReader(in);
br = new BufferedReader(inr);
while(br.read()!=-1){
str = br.readLine();
System.out.println(str);
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
in.close();
inr.close();
br.close();
}catch(IOException e2){
e2.printStackTrace();
}
}
}
public static void main(String[] args) {
PubFunction p = new PubFunction();
p.readTxtFile("D:\\new_selfcard\\cardfile\\yangli1025-21-01-20100716-110757.txt");
}
二、得到目录下文件数及文件名
代码如下:
File f = new File(bxtkPath); //bxtkPath是目录
if(f.exists()){
String[] list = f.list();
int j = list.length; //文件个数
for(int i=0;i<list.length;i++){
pdfname = list[i]; //文件名称
}
}