标签:JAVA String filePath int --- File new 东软 Calendar
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
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。