黑客常用工具下载大全
2010-06-15 14:07
295 查看
根据String分为:输入流:InputStream,Reader(可正确转换字符编码)
输出流:OutStream,Writer(可正确转换字符编码)
方法:.Inputstream类中的常用方法:
(1) public abstract int read();读取一个byte的数据。
(2) public int read(byte b[]);读取一个长度为b.length的字节数组。返回
值是读取的字节数。
(3) public int read(byte b[],int off,int len); 从输入流中最多读取
len个字节的数据,存放到偏移量为off的b数组中。
(4) public int available();返回输入流中可以读取的字节数。注意:若输入阻
塞,当前线程将被挂起,如果InputStream对象调用这个方法的话,它只会返回
0,这个方法必须由继承InputStream类的子类对象调用才有用。
(5) public long skip(long n); 忽略输入流中的n个字节,返回值是实际忽略的
字节数, 跳过一些字节来读取。
(6) public int close();在使用完后,必须对我们打开的流进行关闭。
/**
* 从文件中读取数据
*
* @param path
* :要读取的文件路径
* @return 将读取到的数据以字符串格式返回
*/
public String readFile(String path) {
try {
// 根据文件路径创建一个文件输入流
java.io.FileInputStream fis = new java.io.FileInputStream(path);
// 得到流中字节数
int len = fis.available();
// 根据字节数创建一个字节数组
byte[] bs = new byte[len];
// 读取一个字节
int t = fis.read();
int i = 0;
while (t != -1) {// 如果没有到达文件末尾,就循环读取
// 将读取到的字节放入字节数组中
bs[i] = (byte) t;
// 继续读取下一个字节
t = fis.read();
i++;
}
// 关闭流
fis.close();
// 将字节数组转成一个字符串
String str = new String(bs);
return str;
} catch (Exception ef) {
ef.printStackTrace();
}
return null;
}
方法:.Outputstream类中的常用方法:
(1)public void write(byte b[]);将参数b中的字节写到输出流。
(2)public void write(byte b[ ], int off, int len)); 将参数b的从偏移量off开始的len个字节写到输出流。
(3)public abstract void write((int b); 先将int转换为byte类型,把低字节写入到输出流中。
(4)public void flush();将数据缓冲区中数据全部输出,并清空缓冲区。
(5)public void close();关闭输出流并释放与流相关的系统资源。
/**
* 将指定的数据写入指定的文件中
*
* @param path
* :要写入数据的文件
* @param content
* 要写入的数据
*/
public void writeFile(String path, String content) throws Exception{
// 创建一个文件输出流,如果文件不存在,会自动创建一个文件
java.io.FileOutputStream fos = new java.io.FileOutputStream(path);
// 将字符串转成字节数组
byte[] bs = content.getBytes();
// 遍历数组,取出字节,写到流中
for (int i = 0; i < bs.length; i++) {
// 写出字节
fos.write(bs[i]);
}
// 强制输出
fos.flush();
// 关闭流
fos.close();
}
//练习完成文件的复制
public class FileCopy {
public static void main (String []args) {
FileCopy fc = new FileCopy();
String srcpath = "D:\\test.jpg";
String destpath = "D:\\newfile.jpg";
fc.copy(srcpath, destpath);
System.out.println("复制结束");
}
/**
* 完成图片的复制
* @param srcpath 源文件地址
* @param destpath 目标地址
*/
public void copy(String srcpath, String destpath) {
try{
//根据源文件地址创建文件输入流
java.io.FileInputStream ins = new java.io.FileInputStream(srcpath);
//根据目标文件地址创建文件输出流
java.io.FileOutputStream ots = new java.io.FileOutputStream(destpath);
int t = ins.read();// 从输入流中读取数据的下一个字节。
while (t != -1) {//若没有到文件末尾就输出
ots.write(t);
t = ins.read();//读取下一个字节
}
//强制输出
ots.flush();
//关闭流
ins.close();
ots.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
异常处理测试
public class ExceptionTest {
public static void main (String [] args) throws Exception {
ExceptionTest et = new ExceptionTest();
et.Test(110);
}
public void Test(int t) throws Exception {
if (t >= 0 && t < 100) {
System.out.println("输入正常");
} else {
//创建异常对象
Exception ep = new Exception("输入异常");
throw ep;
}
}
}
输出流:OutStream,Writer(可正确转换字符编码)
方法:.Inputstream类中的常用方法:
(1) public abstract int read();读取一个byte的数据。
(2) public int read(byte b[]);读取一个长度为b.length的字节数组。返回
值是读取的字节数。
(3) public int read(byte b[],int off,int len); 从输入流中最多读取
len个字节的数据,存放到偏移量为off的b数组中。
(4) public int available();返回输入流中可以读取的字节数。注意:若输入阻
塞,当前线程将被挂起,如果InputStream对象调用这个方法的话,它只会返回
0,这个方法必须由继承InputStream类的子类对象调用才有用。
(5) public long skip(long n); 忽略输入流中的n个字节,返回值是实际忽略的
字节数, 跳过一些字节来读取。
(6) public int close();在使用完后,必须对我们打开的流进行关闭。
/**
* 从文件中读取数据
*
* @param path
* :要读取的文件路径
* @return 将读取到的数据以字符串格式返回
*/
public String readFile(String path) {
try {
// 根据文件路径创建一个文件输入流
java.io.FileInputStream fis = new java.io.FileInputStream(path);
// 得到流中字节数
int len = fis.available();
// 根据字节数创建一个字节数组
byte[] bs = new byte[len];
// 读取一个字节
int t = fis.read();
int i = 0;
while (t != -1) {// 如果没有到达文件末尾,就循环读取
// 将读取到的字节放入字节数组中
bs[i] = (byte) t;
// 继续读取下一个字节
t = fis.read();
i++;
}
// 关闭流
fis.close();
// 将字节数组转成一个字符串
String str = new String(bs);
return str;
} catch (Exception ef) {
ef.printStackTrace();
}
return null;
}
方法:.Outputstream类中的常用方法:
(1)public void write(byte b[]);将参数b中的字节写到输出流。
(2)public void write(byte b[ ], int off, int len)); 将参数b的从偏移量off开始的len个字节写到输出流。
(3)public abstract void write((int b); 先将int转换为byte类型,把低字节写入到输出流中。
(4)public void flush();将数据缓冲区中数据全部输出,并清空缓冲区。
(5)public void close();关闭输出流并释放与流相关的系统资源。
/**
* 将指定的数据写入指定的文件中
*
* @param path
* :要写入数据的文件
* @param content
* 要写入的数据
*/
public void writeFile(String path, String content) throws Exception{
// 创建一个文件输出流,如果文件不存在,会自动创建一个文件
java.io.FileOutputStream fos = new java.io.FileOutputStream(path);
// 将字符串转成字节数组
byte[] bs = content.getBytes();
// 遍历数组,取出字节,写到流中
for (int i = 0; i < bs.length; i++) {
// 写出字节
fos.write(bs[i]);
}
// 强制输出
fos.flush();
// 关闭流
fos.close();
}
//练习完成文件的复制
public class FileCopy {
public static void main (String []args) {
FileCopy fc = new FileCopy();
String srcpath = "D:\\test.jpg";
String destpath = "D:\\newfile.jpg";
fc.copy(srcpath, destpath);
System.out.println("复制结束");
}
/**
* 完成图片的复制
* @param srcpath 源文件地址
* @param destpath 目标地址
*/
public void copy(String srcpath, String destpath) {
try{
//根据源文件地址创建文件输入流
java.io.FileInputStream ins = new java.io.FileInputStream(srcpath);
//根据目标文件地址创建文件输出流
java.io.FileOutputStream ots = new java.io.FileOutputStream(destpath);
int t = ins.read();// 从输入流中读取数据的下一个字节。
while (t != -1) {//若没有到文件末尾就输出
ots.write(t);
t = ins.read();//读取下一个字节
}
//强制输出
ots.flush();
//关闭流
ins.close();
ots.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
异常处理测试
public class ExceptionTest {
public static void main (String [] args) throws Exception {
ExceptionTest et = new ExceptionTest();
et.Test(110);
}
public void Test(int t) throws Exception {
if (t >= 0 && t < 100) {
System.out.println("输入正常");
} else {
//创建异常对象
Exception ep = new Exception("输入异常");
throw ep;
}
}
}
相关文章推荐
- 黑客常用工具下载大全
- js大全《二》 包含 js拖拽 打印 下载 操作收藏栏等IE工具 等常用js
- js大全《二》 包含 js拖拽 打印 下载 操作收藏栏等IE工具 等常用js
- 常用黑客工具下载
- 常用工具下载地址大全
- 黑客常用命令大全
- 常用工具下载地址汇总
- Android 工具类】常用工具类(方法)大全
- Ubuntu 13.0 嵌入式开发常用工具安装(二) u-boot和Kernel及busybox官方下载地址
- MySQL命令大全:MySQL常用命令手册、MySQL命令行大全、查询工具
- java/.net-常用工具下载地址&常用学习网址
- 黑客基础知识 常用命令和快捷键大全
- 黑客 专题二 常用工具二
- 黑客必备工具分类大全(精)
- 常用各种软件+文档+工具 下载地址
- asp.net下载文件的常用方法大全
- 常用.NET工具下载(包括.NET可再发行包2.0)
- 嵌入式Linux常用工具下载官网
- 【iphone游戏开发】iPhone游戏开发之Cocos2d常用工具大全
- 100多个经典常用的jQuery插件大全实例演示和下载