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文件读写操作大全java文件读写操作大全java文件读写操作大全java文件读写操作大全java文件读写操作大全
我自己搜集的用java写的操作文件和目录的程序,挺全的
自己用java编写的一个模拟DOS文件目录操作的小程序。有dir, cd, cd/, cd.., md, rd等功能。
java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java...
java中文件操作大全 覆盖大部分对文件操作的基本代码
自己写的java中文件的操作工具类,包括对目录的管理
java中文件操作大全 利用PrintStream写文件 文件重命名 转移文件目录
java操作Excel文件java操作Excel文件java操作Excel文件java操作Excel文件java操作Excel文件java操作Excel文件java操作Excel文件
# java实现对文件的各种操作的工具类 ## 可以实现的操作有: 1. 删除单个文件 2. 删除文件夹及文件夹下的文件 3. 使用文件流对单个文件进行复制 4. 复制整个文件夹内容(包含子文件夹中的所有内容) 5. ...
java读取共享文件使用jcifs-1.1.11.jar 的SmbFile 类使用用户名密码共享目录操作共享文件
java实现文件读写操作,小例子,供大家参考
用java8实现一个文件目录的监听,目录中文件创建、修改、删除等操作
java视频教程 Java文件操作 JavaFile
内容包括java语言对文件和文件夹的各种操作
Java中文件或目录操作.pdf 学习资料 复习资料 教学资源
java 遍历文件目录,区分文件夹及问价类型,方便开发人员对文件操作,用于流方式的拷贝文件
java 中关于文件的操作 比较基础 比较实用
io java 文件操作 命令 全部 io java 文件操作 命令 全部 io java 文件操作 命令 全部 io java 文件操作 命令 全部
Java文件操作大全
java文件操作总结 详细介绍 java对文件的操作 每个都有对应的例子