javaIO读和写和创建文件夹的基础简单操作
导语:Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的`多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。下面我们来看看javaIO读和写和创建文件夹的基础简单操作,希望对大家有所帮助。
1234567package com.zuoye16;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
1public
class FileUtil
public
static
final String PATH=System.getProperty
"user.dir"
;
public
void cjString path File file=new FilePATH+path;
int
index
=path.indexOf
"."
; if
index
==-1 if!file.exists file.mkdirs;
else
int
index2=path.lastIndexOf
"/"
; String string=path.
substring
0, index2; if!file.exists File file2=new FilePATH+string; file2.mkdirs; try file.createNewFile; catch IOException e // TODO Auto-generated catch block e.printStackTrace;
public class TestFileUtil public static void mainString[] args
1//读和写的初始化 FileReader fileReader=
null
; FileWriter fileWriter=
null
; try
1234567891011
//读和写的初始化
fileReader=new FileReaderfile;
fileUtil.cj
"/src/com/lanou/day18/Text.txt"
;
fileWriter=new FileWriterfileName;
//将读出来的数据写入
int
lenght=0;
char
[] context=new
char
[1024];
whilelenght=fileReader.
read
context!=-1
fileWriter.writecontext,0,lenght;
1234567891011
//完成后刷新
fileWriter.flush;
catch FileNotFoundException e
// TODO Auto-generated catch block
e.printStackTrace;
catch IOException e
// TODO Auto-generated catch block
e.printStackTrace;
finally
iffileWriter!=
null
try
123456789101112
//关闭写入流
fileWriter.
close
;
catch IOException e
// TODO Auto-generated catch block
e.printStackTrace;