Java中一般File文件的读写
创建文件:
file.createNewFile();
public static void main(String[] args) {
File file = new File(FileUtil.FILE_NAME);
if (!file.exists()) {
try {
//创建文件
file.createNewFile();
System.out.println("文件已经创建");
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("文件以存在");
System.out.println("文件名:" + file.getName());
System.out.println("文件绝对路径:" + file.getAbsolutePath());
System.out.println("文件大小:" + file.length() + "字节");
System.out.println("文件是否可读:" + file.canRead());
System.out.println("文件是否可写:" + file.canWrite());
System.out.println("文件是否隐藏:" + file.isHidden());
}
}
文件重命名:
public static void main(String[] args){
File file = new File(FileUtil.FILE_NAME);
File newFile = new File("anotherFile.txt");
file.renameTo(newFile);//文件重命名
}
获取文件名 file.getName();
获取相对路径 file.getPath();
获取绝对路径 file.getAbsolutePath();
删除文件 file.delete();
文件大小--字节 file.length();
可读 file.canRead();
可写 file.canWrite();
隐藏 file.isHidden();
重命名 file.renameTo(newFile);
创建文件夹
public static void main(String[] args) {
//创建文件夹
File folder = new File("Folder");
if (!folder.exists()) {
folder.mkdir();
}
//创建多级目录
File newFolder = new File("NewFolder" + File.separator + "nextFolder");
if (!newFolder.exists()) {
newFolder.mkdirs();
}
}
创建文件夹 folder.mkdir();
创建多级目录 newFolder.mkdirs();
File.separator是指分隔符"/"
注意:只能删除没有子目录的文件夹 folder.delete();
读取assets和raw中的文件数据
读取assets中的文件数据
在src/main 下新建assets文件夹,在文件夹中放入以 UTF-8 编码的 info.txt文件:
1.这是 assets 第一行文字
2.这是第二行
3.这是第三行
4.这是第四行
1.首先使用 getResources().getAssets().open("info.txt")方法 打开assets中文件;
2.接着,使用 InputStreamReader 将字节流转换为字符流,并指定编码格式;
3.然后,调用 bufferedReader.readLine() 逐行读取数据
//assets数据读取
findViewById(R.id.btn_readassets).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
//打开assets中文件
InputStream inputStream = getResources().getAssets().open("info.txt");
//字节流转换为字符流,( ,指定编码)
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
// Log.i(TAG, bufferedReader.readLine());
String in = "";
//逐行读取数据
while ((in = bufferedReader.readLine()) != null) {
Log.i(TAGA, in);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
读取raw中的文件数据
在res下新建文件夹raw,并在文件夹中放入以 UTF-8 编码的 info.txt文件:
1.这是 raw 第一行文字
2.这是第二行
3.这是第三行
4.这是第四行
1.首先使用 getResources().openRawResource(R.raw.info)方法 打开raw中文件;
其他均与与assets类似,
//raw数据读取
findViewById(R.id.btn_readraw).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
InputStream is = getResources().openRawResource(R.raw.info);
InputStreamReader isr = new InputStreamReader(is,"UTF-8");
BufferedReader br = new BufferedReader(isr);
String in = "";
while ((in = br.readLine()) != null){
Log.i(TAGR, in);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
效果图:
具体代码点击
写入以及读取内部存储的数据
这里分为两部分,
首先,是写入操作:
1. 使用 openFileOutput() 方法打开文件,
2.OutputStreamWriter()转换为字符输入格式,
3.写入、刷新、关闭
//写入文件操作
findViewById(R.id.btn_write).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
//文件输出流
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
//转换为字符输入格式(字节流包装成字符流)
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
//写入
osw.write(et.getText().toString());
//刷新,确保都存入
osw.flush();
fos.flush();
//关闭
osw.close();
fos.close();
Toast.makeText(getApplicationContext(),"写入完成",Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
第二,是读取操作:
1.使用 openFileInput() 方法打开读取文件,
2.OutputStreamWriter()将字节流包装成字符流,
3.读取、关闭、显示
//读取文件操作
findViewById(R.id.btn_read).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
FileInputStream fis = openFileInput(filename);
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
//文件的可用长度:fis.available()
char input[]= new char[fis.available()];
//读取
isr.read(input);
//关闭
isr.close();
fis.close();
//显示
String readStr = new String(input);
show.setText(readStr);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
在DDMS中,File Exploer下,data/data/com.dingding.readwriteinternaldata/files中,可以看到生成的 text 文件,如图:
效果图:
写入以及读取外部存储的数据
首先,要获取外部存储sd卡的路径
File sdcard = Environment.getExternalStorageDirectory();//外部存储sd卡的路径
开始写入数据
1.在sd卡中new一个 File 文件
2.判断sd卡是否存在
3.创建文件 myfile.createNewFile()
4.数据转换为字节并保存到文件,使用 FileOutputStream(myfile)
5.字节流转换为字符流,使用 OutputStreamWriter(fos,"UTF-8")
6.写入、刷新、关闭
//写入数据
findViewById(R.id.btn_write).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File myfile = new File(sdcard, "this my file.txt");
//判断sd卡是否存在
if (!sdcard.exists()) {
Toast.makeText(getApplicationContext(), "当前系统不具备sd卡目录", Toast.LENGTH_SHORT).show();
return;
}
System.out.println(sdcard.getAbsolutePath());
try {
//创建文件
myfile.createNewFile();
Toast.makeText(getApplicationContext(), "文件创建完成", Toast.LENGTH_SHORT).show();
//写数据到文件,将数据转换成字节并将其保存到文件
FileOutputStream fos = new FileOutputStream(myfile);
//字节流转为字符流
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
//写入
osw.write(et.getText().toString());
//刷新
osw.flush();
fos.flush();
//关闭
osw.close();
fos.close();
Toast.makeText(getApplicationContext(), "文件写入完成", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
开始读取数据
1.在sd卡中new一个 File 文件
2.从文件中取出字节流数据,使用FileInputStream(myfile)
3.字节流转换为字符流,使用InputStreamReader(fis,"UTF-8")
4.读取、关闭、显示
//读取数据
findViewById(R.id.btn_read).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//数据存储文件
File myfile = new File(sdcard, "this my file.txt");
if (myfile.exists()) {
try {
FileInputStream fis = new FileInputStream(myfile);
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
char input[] = new char[fis.available()];
//读取
isr.read(input);
//关闭
isr.close();
fis.close();
String str = new String(input);
//显示
show.setText(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
注意:最后还需要添加写入权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
效果图: