import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File fileTest = new File("Fred.txt");
if (fileTest.exists()) {
fileTest.delete();
} else {
byte[] b = "Freddie".getBytes();
byte[] z = new byte[1024];
fileTest.createNewFile();
System.out.println("ok");
System.out.println(fileTest.getAbsolutePath());
FileOutputStream in = new FileOutputStream("Fred.txt");
FileInputStream out = new FileInputStream("Fred.txt");
in.write(b);
int len = out.read(z);
System.out.println(new String(z, 0, len));
}
}
}
此博客展示了一段Java代码,通过import引入相关类,实现文件操作。代码中创建了File对象,判断文件是否存在,若存在则删除,不存在则创建新文件,还进行了字节数组写入和读取操作,并将读取内容转换为字符串输出。
4598

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



