java对文件操作之实用
创建文件
package com.pre;
import java.io.File;
public class WJ
{
public static void main(String[] args) throws Exception
{
File file=new File("D:"+File.separator+"text2.txt");//创建路径下的文件
if(!file.getParentFile().exists())//如果父路径不存在
{
file.getParentFile().mkdirs();//创建父路径
}
if(file.exists())//如果文件存在
{
file.delete();//删除文件
}
else {
System.out.println(file.createNewFile());//否则创建文件
}
}
}
列出D盘的所有文件及目录:
package com.pre;
import java.io.File;
public class Pd
{
public static void main(String [] args)
{
File file=new File("D:"+File.separator);//创建路径
print(file);
}
public static void print(File file)//递归函数依次判断如果为目录,继续执行,如果为文件则输出
{
if(file.isDirectory())//如果file为目录
{
File rest[]=file.listFiles();//列出目录
if(rest!=null)//如果目录不为空
{
for(int i=0;i<rest.length;i++)
{
print(rest[i]);
}
System.out.println(file);
}
}
}
}
*if(rest!=null)不能删除,否则报错有空值
如果把System.out.println(file);改为file.delete();则功能会变为将D盘所有文件删除。
使用字节流操作文件的读取和写入操作:
package com.pre;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class WJ
{
public static void main(String[] args) throws Exception
{//首先创建文件的路径
File file=new File("D:"+File.separator+"text3.txt");
OutputStream output=new FileOutputStream(file,true);//然后实例化OutputStream类的对象,追加型添加数据
String str="今天早上真是个好天气\r\n";
byte data[]=str.getBytes();//将String类型转换为byte类型
output.write(data);//进行写入操作
output.close();//关闭流
InputStream input=new FileInputStream(file);//实例化InputStream类的对象
byte b[]=new byte[1024];//建立数组存储从文件中读取的数据
int aa=0;//存取每个读取的数据
int foot=0;//定义索引
while((aa=input.read())!=-1)//如果文件中的数据没有读取完(返回值不为-1)便转换为byte类型赋值给foot
{
b[foot++]=(byte)aa;
}
System.out.println("读取的内容为:【"+new String(b,0,foot)+"】");//输出读取的内容
}
}
使用字符流操作文件的写入和读取操作:
package com.pre;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
public class WJ
{
public static void main(String[] args) throws Exception
{//首先创建文件的路径
File file=new File("D:"+File.separator+"text3.txt");
//向文件写入内容
if(!file.getParentFile().exists())
{
file.getParentFile().mkdirs();
}
Writer output=new FileWriter(file,true);
String aa="今天的天气真好";
output.write(aa);
output.close();
//从文件读取内容
if(file.exists())
{
Reader input=new FileReader(file);
char bb[]=new char[1024];
int len=input.read(bb);
System.out.println("从文件中读取的内容为:【"+new String(bb,0,len)+"】");
input.close();
}
}
}
java对文件操作之实用的更多相关文章
- java常见文件操作
收集整理的java常见文件操作,方便平时使用: //1.创建文件夹 //import java.io.*; File myFolderPath = new File(str1); try { if ( ...
- Java api 入门教程 之 JAVA的文件操作
I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程的一个基本 ...
- java中文件操作的工具类
代码: package com.lky.pojo; import java.io.BufferedReader; import java.io.BufferedWriter; import java. ...
- java csv 文件 操作类
一个CSV文件操作类,功能比较齐全: package tool; import java.io.BufferedReader; import java.io.BufferedWriter; impor ...
- java中文件操作《一》
在日常的开发中我们经常会碰到对文件的操作,在java中对文件的操作都在java.io包下,这个包下的类有File.inputStream.outputStream.FileInputStream.Fi ...
- java IO文件操作简单基础入门例子,IO流其实没那么难
IO是JAVASE中非常重要的一块,是面向对象的完美体现,深入学习IO,你将可以领略到很多面向对象的思想.今天整理了一份适合初学者学习的简单例子,让大家可以更深刻的理解IO流的具体操作. 1.文件拷贝 ...
- Java常用文件操作-2
上篇文章记录了常用的文件操作,这里记录下通过SSH服务器操作Linux服务器的指定路径下的文件. 这里用到了第三方jar包 jsch-0.1.53.jar, jsch-api 1.删除服务器上指定路径 ...
- Java常用文件操作-1
在我们的实际工作中经常会用到的文件操作,再此,将工作中碰到的做一个记录,以便日后查看. 1.复制文件夹到新文件夹下 /** * 复制文件夹下所有文件到指定路径 *@param oldPath *@pa ...
- 【java】文件操作java.io.File
package 文件操作; import java.io.File; import java.io.IOException; public class TestFile { public static ...
随机推荐
- 杭电1004 ac code
#include <stdio.h> #include <string.h> #include <stdlib.h> #define STR_LEN 256 str ...
- VS2012里面使用EF框架的增删改查和分页的方法
public class BaseRepository<T> where T : class { //实例化EF框架 DataModelContainer ...
- 关于selenium实现滑块验证
关于selenium实现滑块验证 python2.7+selenium2实现淘宝滑块自动认证参考链接:https://blog.csdn.net/ldg513783697/article/detail ...
- Day17作业及默写
正则表达式练习 1.匹配一篇英文文章的标题 类似 The Voice Of China ([A-Z][a-z]*)( [A-Z][a-z]*)* 2.匹配一个网址 https://www.baidu. ...
- winform dataGridView中的button点击判断
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.RowI ...
- random_select
package sorttest; //expected and worst running time is O(n),asuming that the elements are distinct ...
- Shiro自定义Realm时用注解的方式注入父类的credentialsMatcher
用Shiro做登录权限控制时,密码加密是自定义的. 数据库的密码通过散列获取,如下,算法为:md5,盐为一个随机数字,散列迭代次数为3次,最终将salt与散列后的密码保存到数据库内,第二次登录时将登录 ...
- FZU 2272 Frog 第八届福建省赛 (鸡兔同笼水题)
Problem Description Therearex frogs and y chicken in a garden. Kim found there are n heads and m leg ...
- zookeeper和dubbo中出现的问题
报错出现timeout关键字 解决:在服务发布时,添加timeout字段 <!-- 5.服务发布 --> <dubbo:service interface="com.sxt ...
- 2.26 js解决click失效问题
2.26 js解决click失效问题 前言有时候元素明明已经找到了,运行也没报错,点击后页面没任何反应.这种问题遇到了,是比较头疼的,因为没任何报错,只是click事件失效了.本篇用2种方法解决这种诡 ...