编程要求
读取src/step2/input/目录下的task.txt文件信息并输出到控制台,使用Java代码将字符串learning practice写入到src/step2/output/目录下的output.txt,若文件目录不存在,则创建该目录。
注意:临时字节数组需要定义长度为8位,否则会有空格。
下面展示一些 内联代码片
。
public class Task {
public void task() throws IOException{
/********* Begin *********/
FileInputStream fs = new FileInputStream("src/step2/input/task.txt");
byte[] b = new byte[8];
fs.read(b);
String str = new String(b);
System.out.println(str);
File dir = new File("src/step2/output/");
if(!dir.exists()){
dir.mkdir();
}
FileOutputStream fos = new FileOutputStream("src/step2/output/output.txt");
String out = "learning practice";
byte[] outByte = out.getBytes(); //将字符串转换成字节
fos.write(outByte); //写数据
//释放资源
fs.close();
fos.close();
/********* End *********/
}
}
这个题烦人的就是他的题目是让你创建目录,先创建目录后,再写文件。。。。。。