import java.io.File
import java.io.IOException
public class Test10 {
public static void main(String[] args) {
//创建“abc”文件夹
String pathName = "d:\\abc"
File path = new File(pathName)
path.mkdir()
//创建“abc”文件
String fileName = "d:\\abc\\abc"
File file = new File(fileName)
try {
file.createNewFile()
}
catch (IOException e) {
e.printStackTrace()
}
}
}
File file = new File("d:/newfile/123.txt")file.getParentFile().mkdirs()//创建目录
file.createNewFile()//创建文件
public static void main(String[] args) {//创建文件夹及文件
createFile()
//复制内容到txt后,读取
readTxt()
}
public static void createFile(){
File dir = new File("D:/JavaTest")
if(!dir.exists()){
dir.mkdir()
}
File text = new File("D:/JavaTest/test.txt")
if(!text.exists()){
try {
text.createNewFile()
} catch (IOException e) {
e.printStackTrace()
}
}
}
public static void readTxt(){
try {
Scanner sc = new Scanner(new File("D:/JavaTest/test.txt"))
while(sc.hasNext()){
System.out.println(sc.next())
}
sc.close()
} catch (FileNotFoundException e) {
e.printStackTrace()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)