Java高级编程 输入和输出处理一
文章目录
一、File文件操作
File类的常用方法
方法名称 | 说明 |
---|---|
boolean exists( ) | 判断文件或目录是否存在 |
boolean isFile( ) | 判断是否是文件 |
boolean isDirectory( ) | 判断是否是目录 |
String getPath( ) | 返回此对象表示的文件的相对路径名 |
String getAbsolutePath( ) | 返回此对象表示的文件的绝对路径名 |
String getName( ) | 返回此对象表示的文件或目录的名称 |
boolean delete( ) | 删除此对象指定的文件或目录 |
boolean createNewFile( ) | 创建名称的空文件,不创建文件夹 |
long length() | 返回文件的长度,单位为字节**,** 如果文件不存在,则返回 0L |
//绝对路径 相对路径
File file = new File("1.text");
System.out.println("判断文件或目录是否存在:"+file.exists());
System.out.println("判断是否是文件:"+file.isFile());
System.out.println("判断是否是目录"+file.isDirectory());
System.out.println("获得相对路径:"+file.getPath());
System.out.println("获得绝对路径:"+file.getAbsolutePath());
System.out.println("获得文件或目录名称:"+file.getName());
System.out.println("获得文件长度,字节数"+file.length());
System.out.println("删除文件或目录"+file.delete());
/*
* 1B字节 = 8bit(位)
* 1KB千字节 = 1024B
*/
String path = "D:\\4021/001";
String name = "1.txt";
try {
File file1 = new File(path);
if(!file1.exists()&&!file1.isDirectory()) {
//进来创建
System.out.println("创建目录");
file1.mkdirs();//创建多级目录 file1.mkdir();创建文件夹||或目录
}
File file2 = new File(path,name);
if(!file2.exists()&&file2.isFile()) {
//进来创建
boolean b = file2.createNewFile();//创建文件
}
} catch (Exception e) {
e.printStackTrace();
}
二、流的分类与使用
Java流的分类
**字节流:**byte
**输入流:**input(读取)
InputStream->(子类为)FileInputStream(路径)
.available():获得文件字节数量
.read(字节数组)||.read() 读取文件
is.close;//关闭流 释放资源
**输出流:**Output(写入)
OutputStream->FilOutputStream(“路径”,boolean 是否追加)
os.write(字符串.get.Bytes(“utf-8”));写入内容 将字符串转换为字节数组 并释放资源
os.close();//释放资源
os.flush(); 强制把缓冲区的数据写到输出流中
**字符流:**char(String)
输入流:Reader
Reader->InputStreamReader(字节流InputStream,编码)->FileReader(路径)
解决乱码问题 Reader a = new InputStreamReader(new FileInputStream(路径),“utf-8”);
缓冲区 BufferedReader(Reader)
br.readLine();
输出流:Writer
Writer->OutputStreamWriter(字节流OutputStream,编码)->FileWriter(路径||file对象)
方法:
write(字符串)写入内容
flush()将缓冲区中的数据 刷新到文件中
close()释放资源
缓冲区BufferedWriter(Writer)
append(字符)写入内容
write(字符串)写入内容
flush() 将缓冲区中的数据 刷新到文件中
close()释放资源
bw.newLine();另起一行
三、代码演示
/**
* 输入流 input 读取文件内容 文件->代码 字节流
* @author 26255
*
*/
public class Text04 {
public static void main(String[] args) {
InputStream is = null;
try {
//创建流
is = new FileInputStream("2.txt");
//创建字节数组 用于储存文件信息
byte b[] = new byte[is.available()];
is.read(b); //读取文件内容
//将字节数组 转换为字符串
String str = new String(b,"utf-8");
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(is!=null)is.close();//关闭流 释放资源
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 输出流 output 写入 代码->文件 字节流
* @author 26255
*
*/
public class Text05 {
public static void main(String[] args) {
try {
//创建流
OutputStream os = new FileOutputStream("2.txt",true);
os.write("zhisha".getBytes("utf-8"));
os.close();//释放资源
}catch(Exception e){
e.printStackTrace();
}
/**
* 读取文件内容 输入流 字符流
* @author 26255
*
*/
public class Text06 {
public static void main(String[] args) {
try {
//Reader reader = new FileReader("2.txt");
//解决中文乱码问题
Reader reader =new InputStreamReader
(new FileInputStream("2.txt"),"uft-8");
//字符数组
char c[] = new char[100];
reader.read(c);
//将数组转换为字符串
String msg = new String(c);
System.out.println(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 读取文件内容 输入流 缓冲区
* @author 26255
*
*/
public class Text07 {
public static void main(String[] args) {
try {
//Reader reader = new FileReader("2.txt");
//解决中文乱码问题
Reader reader = new InputStreamReader
(new FileInputStream("2.txt"),"utf-8");
//创建缓冲区
BufferedReader rb = new BufferedReader(reader);
String line = null;
while((line=rb.readLine())!=null) {
System.out.println(line);
}
rb.close();
reader.close();//结束
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 输出流 写入 字符流
* @author 26255
*
*/
public class Text08 {
public static void main(String[] args) {
try {
//Writer writer = new FileWriter("2.txt");
//解决乱码
Writer writer = new OutputStreamWriter
(new FileOutputStream("2.txt",true),"uft-8");
writer.write("赔过钱");
//writer.append("");
System.out.println("写入成功!!!");
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 输出流 写入 字符流 缓冲区
* @author 26255
*
*/
public class Text09 {
public static void main(String[] args) {
try {
//Writer writer = new FileWriter("2.txt");
//解决乱码
Writer writer = new OutputStreamWriter
(new FileOutputStream("2.txt",true),"uft-8");
//创建缓冲区
BufferedWriter bw = new BufferedWriter(writer);
bw.newLine();//另起一行
bw.append("田楷波");
//将缓冲区中的数据 刷新到文件中
bw.flush();
System.out.println("写入成功!!");
//释放资源
bw.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
四、序列化与反序列化
序列化:内存对象->文件 写入文件
ObjectOutputStream(OutStream字节流)
WriteObject(对象)
反序列化:文件->内存对象 读取文件
ObjectInputStream(InputStream字节流)
readObject()返回对象
注意:序列化对象要实现接口java.io.Serializable
/**
* 序列化 写入
* 反序列化 读取
* @param args
*/
public static void main(String[] args) {
OutputStream os = null;
ObjectOutputStream oos = null;
InputStream is = null;
ObjectInputStream ois = null;
try {
//创建字节流对象
os = new FileOutputStream("D:\\xiangmueclipse\\expertch07\\src\\com\\hz\\ch04\\wwa.txt");
//创建序列化对象
oos = new ObjectOutputStream(os);
List<Dog> list = new ArrayList<Dog>();
//让对象 可序列化
list.add(new Dog("张三","飒飒的萨摩亚"));
//让对象 可序列化
list.add(new Dog("张x","xxxxx"));
oos.writeObject(list);
//创建字节流对象
is = new FileInputStream("3.txt");
//创建反序列化对象
ois = new ObjectInputStream(is);//读取
//Dog d = (Dog)ois.readObject();//读取
//System.out.println(d.toString());
List<Dog> dogs = (List<Dog>) ois.readObject();
for(Dog d:dogs) {
System.out.println(d.toString());
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
os.close();
ois.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
ystem.out.println(d.toString());
List dogs = (List) ois.readObject();
for(Dog d:dogs) {
System.out.println(d.toString());
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
os.close();
ois.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}