1. 使用FileOutputStream向文件中写入内容
package com;
import java.io.*;
import java.nio.charset.Charset;
public class HelloIO {
public static void main(String[] args) throws IOException{
String fileName = "e:\\test.txt";
/*File file = new File(fileName);
//判断文件是否存在,如果存在则删除
if(file.exists()){
System.out.println(String.format("file %s is exists",fileName));
file.delete();
System.out.println("deleted");
}
*/
FileOutputStream out = null;
String content = "hello IO 玉开";
try{
boolean append = true;
out = new FileOutputStream(fileName,append);
String charsetName = "utf-8";
byte[] byteArray = content.getBytes(Charset.forName(charsetName));
out.write(byteArray);
System.out.println("done");
}
finally{
if(out != null){
out.close();
out = null;
}
}
}
}
2. 逐行读取文件内容
import java.io.*;
public class HelloFileReader {
public static void main(String[] args) throws IOException{
String fileName = "E:\\test.txt";
InputStreamReader rdr = null;
try{
FileInputStream stream = new FileInputStream(fileName);
rdr = new InputStreamReader(stream,"utf-8");
//逐行读取类
LineNumberReader lineRdr = new LineNumberReader(rdr);
String line;
while((line = lineRdr.readLine()) != null){
System.out.println(line);
}
stream.close();
}finally{
if(rdr != null){
rdr.close();
rdr = null;
}
}
}
}
3. 创建或者列出文件目录
package org.zyk;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
public class HelloJavaDir {
public static void main(String[] args) throws IOException{
String dir = "E:/test/test";
File file = new File(dir);
if(!file.exists()){
//mkdirs可以创建多级目录
file.mkdirs();
System.out.println("create new dir");
}else{
System.out.println("dir is exists");
//这儿删除只是删除了最后一级目录
//file.delete();
//System.out.println("delete it");
}
/*在不确认目录是否存在的情况下创建文件*/
String fileName ="e:/test/test/test.txt";
File testFile = new File(fileName);
if(testFile.exists()){
System.out.println("testFile is exists");
}else{
File testParentFile = testFile.getParentFile();
if(!testParentFile.exists()){
//使用mkdirs可以创建多级目录
testParentFile.mkdirs();
}
testFile.createNewFile();
System.out.println("创建了testFile");
}
Date now = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(now);
int year = cal.get(Calendar.YEAR);
String strYear = String.valueOf(year);
String dirYearName = dir + File.separator + strYear;
//创建年目录
File dirYear = new File(dirYearName);
if(!dirYear.exists()){
dirYear.mkdir();
System.out.println("create year dir");
}else{
System.out.println("year dir exists");
}
String yearDefaultFileName = dirYearName + File.separator + "default.htm";
File yearDefaultFile = new File(yearDefaultFileName);
if(yearDefaultFile.exists()){
System.out.println("yearDefaultFile exists");
}else{
yearDefaultFile.createNewFile();
System.out.println("yearDefaultFile created");
}
//创建12个月的目录
for(int month = 1;month<=12;month++){
String monthDirName = dirYearName + File.separator + String.format("%02d",month);
File dirMonth = new File(monthDirName);
//创建月的目录
if(!dirMonth.exists()){
dirMonth.mkdir();
System.out.println("create month dir");
}else{
System.out.println("month dir exists");
}
//创建月的文件
for(int fileIndex = 0;fileIndex<5;fileIndex ++){
String monthFileName;
if(fileIndex == 0)
monthFileName = monthDirName + File.separator + "default.html";
else
monthFileName = monthDirName + File.separator + String.format("list-%d.html",fileIndex);
File fileMonth = new File(monthFileName);
if(!fileMonth.exists()){
fileMonth.createNewFile();
System.out.println("create month file");
}else{
System.out.println("month file exists");
}
}
}
//输出year目录下的所有文件和目录
String[] yearChildren = dirYear.list();
System.out.println("children of the yeardir :");
for(String cFilePath : yearChildren){
System.out.println("\t" + cFilePath);
}
//输出year目录下的所有文件和目录
File[] yearChildrenFiles = dirYear.listFiles();
System.out.println("children files of the yeardir :");
for(File cFile :yearChildrenFiles){
System.out.println("\t" + cFile.getAbsolutePath());
}
//输出year目录下的所有子目录
//注意这个java抽象类实现,很方便
FilenameFilter filterForFolder = new FilenameFilter(){
@Override
public boolean accept(File dir, String name) {
File child = new File(dir.getAbsolutePath() + File.separator + name);
return child.isDirectory();
}
};
String[] filters = dirYear.list(filterForFolder);
for(String filter : filters){
System.out.println(filter);
}
}
}

本文详细介绍如何使用Java进行文件操作,包括使用FileOutputStream向文件写入内容、逐行读取文件内容以及创建或列出文件目录等实用技巧。

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



