JAVA学习笔记---东软
作者:互联网
package daily;
import java.io.*;
import java.util.Calendar;
public class test3_file {
public static void main(String[] args) throws IOException {
//获取系统时间
Calendar today = Calendar.getInstance();
int year = today.get(Calendar.YEAR);
int month =today.get(Calendar.MONTH)+1;//注意
int day = today.get(Calendar.DAY_OF_MONTH);
String y = ""+year;
String m = ""+month;
String d = Integer.toString(day);
// 创建一个目录
String filePath ="D:\\"+y+"\\"+m+"\\"+d+"";
File f =new File(filePath);
if (!f.exists()){
f.mkdirs();
System.out.println("创建完成!");
}
else{
System.out.println("该目录已经存在!");
}
// 向文件中存入数据
// 自动创建文件
File file = new File(filePath,"test.txt");
Writer writer = new FileWriter(file);//此处需要抛出异常
writer.write("你好!世界!!");
writer.close();
// 从文件中读取数据
File file2 = new File(filePath,"test.txt");
Reader reader = new FileReader(file2);
char [] c = new char[1024];
int len = reader.read(c);
String str = "";
while(len!=-1){
str=new String(c,0,len);
len = reader.read(c);
}
System.out.println(str);
}
}
标签:JAVA,String,filePath,int,---,File,new,东软,Calendar 来源: https://www.cnblogs.com/maorui0/p/16025809.html