Java课后总结16(提取,搜索字符串的方法;File类;InputSream与OutputSream)

String类中提取,搜索字符串的方法

public int indexOf(int ch)                          搜索并返回第一个出现字符(ch)的位置

public int indexOf(String vaule)                搜索并返回第一个出现字符串(value)的位置

public int lastIndexOf(int ch)                   搜索并返回最后一个出现字符(ch)的位置

public int lastIndexOf(String vaule)          搜索并返回最后一个出现字符串(value)的位置

public String substring(int index)            提取从指定索引开始的部分字符串

public String substring(int beginindex,int endindex)        提取beginindex和endindex之间的字符串

public String trim()                                  截取字符串前后的空格后返回新的字符串

split(separator, limit)                               根据指定的字符串作为截取点,返回截取的各段字符串

File类

引入File类: import java.io.File;

构造一个文件对象:File file =new File("d:/test.txt");

File类方法:

file.exists();                                                  //判断文件或则目录是否存在,boolean类型

file.isFile();                                                   //判断是时候是文件

file.createNewFile();                                    //创建空的文件

file.getName()                                            //获取文件名称

file.getAbsolutePath()                                //获取绝对路径(详细路径)

file.getPath()                                              //获取相对路径(当前的路径)

file.length()                                                //获取文件大小(单位:字节)

参考代码:

public class Test {
public static void main(String[] args) {
File file =new File("d:/test.txt");   //构造一个文件对象
if(!file.exists()) {                    //判断是否有这个文件
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}else {
System.out.println("文件已存在");
}

String a=file.getAbsolutePath();          //获取绝对路径(详细路径)

String b=file.getPath();                        //获取相对路径(当前的路径)

String name=file.getName();                  //获取文件名称

System.out.println("文件的名字是"+name+"文件的绝对路径的"+b+"\t"+"文件的绝对路径的"+a);

if(file.isFile()) {                      //判断是file是个文件
System.out.println("是一个文件");
}else {
System.out.println("不是文件");
}
}

}

InputStream类

实例化:一般选用它的子类FileInputStream

文件写入流,把数据用字节流的方式从源文件读取到内存中

构造方法:

File InputSream(File file)                 //读取文件的文件名

File InputSream(String name)       //读取文件的地址名

特有方法:

read()                             //逐个读取源文件字节,返回int代表读取的数据字节;读取完该文件,则返回 -1 

read(byte[] b)                          //字节读取到一个数组中,int 返回实际读取字节数

read(byte[] b ,int off,int len)    //把字节读到数组中,从数组下标开始读len个字节,int返回实际读取字节数

close()                                      //关闭输入流

available()                      //返回字节流可读取的字节数

参考代码:
FileInputStream fis=null;
try {
fis=new FileInputStream("F:\\DADA.txt");          //引用指定文件

System.out.println("文件有多少个字节:"+fis.available());   //获取字节流可读取的字节数

int num;         //声明一个变量接受读取返回出来的值
while ((num=fis.read())!=-1) {                    
    System.out.print((char)num); //num返回出来的是ascii码,需要使用Char变相应字符
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fis.close();        //关闭数据流
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

OutputStream类

实例化:一般选用它的子类FileOutputStream

构造方法:

File OutputSream(File file)                 //写入文件的文件名

File OutputSream(String name)       //写入文件的地址名

特有方法:

write()                             //逐个将字节写入目标文件

write(byte[] b)                          //将数组中的字节写入目标文件

write(byte[] b ,int off,int len)    //将数组中的字节写入目标文件,写入长度为len,从off开始

close()                                      //关闭输出流

fulsh()                      //清空缓冲区

参考代码:

FileOutputStream fos=null;
try {
fos=new FileOutputStream("F:\\DADA.txt",true);   //引用指定写入的文件,(true:代表在文件内容后追加字符)
String str="好好学习,天天向上";      //创建一个字符串
byte[]b= str.getBytes();    
             //将字符串转好为字节数组存储
fos.write(b, 0, b.length);                 //用write方法写入
System.out.println("文件已经更新");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fos.close();             //关闭数据流
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值