首页 技术 正文
技术 2022年11月14日
0 收藏 862 点赞 4,324 浏览 4187 个字

1、File类常见方法:

创建:

boolean createNewFile():在指定位置创建文件

如果该文件已经存在,则不创建,返回false,和输出流不一样,输出流对象一建立就创立文件,而且文件已经存在会发生覆盖

boolean mkdir():创建文件夹,只能创建一级文件夹

boolean mkdirs():创建多级文件夹

renameTo():

删除:

boolean delete():删除失败,返回false

void deleteOnExit():在程序退出时删除指定文件

判断:

boolean exists():判断文件是否存在

boolean isFile():

boolean isDirectory():

在判断文件对象是否是文件或者目录时,必须要先判断该文件对象封装的内容是否存在,通过exists()方法进行判断

isHidden():是否为隐藏文件

isAbsolute():是否为绝对路径

获取信息:

getName():

String getPath():

getParent():返回的是绝对路径中的文件父目录,如果获取的是相对路径,则返回空,如果相对路径中有上一层目录,那么该目录就是返回结果

String getAbsolutePath():

lastModified():

length():返回文件长度

Static File[] listRoots():返回系统中所有的盘符

String [] list():返回该路径下所有的文件和目录——FilenameFilter()

File[] listFiles():

Properties类

存取配置文件

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Properties props=new Properties();
FileInputStream fis=new FileInputStream("props.txt");
props.load(fis); Set<String> keys=props.stringPropertyNames();
for(String key:keys){
String value=props.getProperty(key);
System.out.println(key+":"+value);
} FileOutputStream fos=new FileOutputStream("props.txt");
props.setProperty("name", "hello");
props.store(fos, null);
}

打印流

字节打印流:
PrintStream

构造函数可以接收的参数类型

1.file对象

2.字符串路径

3.字节输出流:OutputStream

字符打印流
PrintWriter

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(System.out,true);
String line= br.readLine();
pw.println(line.toUpperCase());
pw.flush(); pw.close();
br.close();
/*while(!"over".equals(line)){
pw.print(line.toUpperCase());
line=br.readLine();
}*/
}

合并流
SequenceInputStream

对象序列化(被操作的对象需要实现Serializable接口)

ObjectInputStream,ObjectOutputStream

        FileOutputStream fos = new FileOutputStream("person.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
Person person=new Person("lvjy",30);
oos.writeObject(person);
oos.close(); FileInputStream fis = new FileInputStream("person.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Person p=(Person) ois.readObject();
System.out.println(p);
ois.close();

自定义版本号:public staitc final long serialVersionUID=42L

新的类还能操作曾经被序列化的对象

只能把堆里面的数据序列化,但是不能把其他区域中的数据序列化(static变量不能序列化)

对非静态的成员也不想序列化的话,可以在序列化对象相关的成员变量加上trainsient关键字

管道流

PipedInputStream,PipedOutputStream

package com.travelsky.io;import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;public class PipedStreamDemo {@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
PipedInputStream in=new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
in.connect(out);PipedOut po = new PipedOut(out);
PipedIn pi = new PipedIn(in);new Thread(po).start();
new Thread(pi).start();
}}
class PipedOut implements Runnable{
private PipedOutputStream pos;
public PipedOut(PipedOutputStream pos) {
super();
this.pos = pos;
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("piped out sleep");
try {
Thread.sleep(5000);
pos.write("hello,world".getBytes());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
class PipedIn implements Runnable{
private PipedInputStream pis;public PipedIn(PipedInputStream pis) {
super();
this.pis = pis;
}@Override
public void run() {
// TODO Auto-generated method stub
byte[] buf =new byte[1024];
try {
System.out.println("piped in wait");
int len=pis.read(buf);
String content = new String(buf,0,len);
System.out.println(content);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

RandomAccessFile

该类不算是IO体系中的子类,而是直接继承自Object

但是它是IO包中成员,因为它具备读和写功能

内部封装了一个数组,而且通过指针对数组的元素进行操作。

可以通过getFilePointer获取指针位置,同时可以通过seek改变指针的位置

通过构造函数可以看出,该类只能操作文件

RandomAccessFile(String name,String mode)  

mode:r,rw,rws,rwd

write方法只写出int类型的最低8位

操作基本数据类型的流对象:DataStream

DataInputStream,DatOutputStream

readInt(),readBoolean,readDouble()

writeInt(),writeBoolean,writeDouble()

writeUTF(),readUTF():加强版的UTF-8

操作字节数组

ByteArrayInputStream:

在构造的时候,需要接收数据源,而且数据源是一个字节数组

ByteArrayOutputStream

在构造的时候,不用定义数据目的,因为该对象中已经内部封装了一个可变长度的字节数组。这就是数据目的地。因为这两个流对象都操作的是数组,并没有使用系统资源,所以不用进行close关闭

操作字符数组

CharArrayReader与CharArrayWriter

操作字符串

StringReader,StringWriter

Tomcat默认的解码方式为ISO8859-1

get方式提交:必须手工转换字符编码

post方式提交:可以通过setCharacterEncoding()函数来设定编码方式

UTF-8修改版:

单字节:0xxxxxxx

两字节:110xxxxx 10xxxxxx

三字节:1110xxxx 10xxxxxx 10xxxxxx

下一篇: Two (DP)
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289