import java.io.*;
public class JavaIo {
public static void main(String[] args) throws IOException {
System.out.println("系统的编码为" + System.getProperty("file.encoding"));
System.out.println("系统的分隔符为" + File.separator);
readfilecontent();
byteStreaminputandoutput();
charStreaminputandoutput();
charStreamread();
}
/**
* 字节流,InputStream(输入) 是一个接口,子类实例化对象FileInputStream
* 1.1 读取文件里面的内容,并打印出来
*/
public static void readfilecontent() throws IOException {
File file = new File("D:\\test\\kaishi.txt");
InputStream input = null;
input = new FileInputStream(file);
byte b[] = new byte[10240];
// 优化上面这里 byte b[] = new byte[(int)file.length()];
input.read(b);
input.close();
System.out.printf("第二个内容为:" + new String(b));
}
/**
* 字节流 ,OutputStream(输出)
* 1.2先文件中写入字符串
*/
public static void byteStreaminputandoutput() throws IOException {
File file = new File("D:\\test\\kaishi2.txt");
String hello ="asdfjkldsafkldjskhehello,这是字节流 InputStream(输入),OutputStream(输出)demo\n" +
"OutputStream是一个接口,子类实例化对象FileOutputStream";
OutputStream out=null;
out =new FileOutputStream(file);
//末尾追加内容 out =new FileOutputStream(file,true);
byte[] b = hello.getBytes();
out.write(b);
out.close();
}
/**
* 字符流,在程序中,一个字符=两个字节,专门有 Reader 和Writer 专门操作字符流
* 2.1 先文件中写入数据
* 和1.1 比较,唯一的好处是 直接输出字符串,不用将字符串便为byte数组之后再输出
*/
public static void charStreaminputandoutput() throws IOException {
File file = new File("D:\\test\\kaishi3.txt");
String hello ="asdfjkldsafkldjskhehello,和1.1 比较,唯一的好处是 直接输出字符串,不用将字符串便为byte数组之后再输出\n" +
"在程序中,一个字符=两个字节,专门有 Reader 和Writer 专门操作字符流";
Writer out =null;
out =new FileWriter(file);
// 不需要字节流的 byte[] b = hello.getBytes();
out.write(hello);
out.flush();
// OutputStream out=null;
// out =new FileOutputStream(file);
// //末尾追加内容 out =new FileOutputStream(file,true);
// byte[] b = hello.getBytes();
// out.write(b);
// out.close();
}
/**
* 字符流,在程序中,一个字符=两个字节,专门有 Reader 和Writer 专门操作字符流
* 2.2 先文件中读取数据 并打印出来
* */
public static void charStreamread() throws IOException {
File file = new File("D:\\test\\kaishi4.txt");
Reader readercontent =null;
readercontent =new FileReader(file);
// char[] c =new char[1024];
// readercontent.read(c);
// int len =readercontent.read(c);
// readercontent.close();
// System.out.printf("读取内容为:"+new String(c,0,len));
int len = 0;
char c[] =new char[1024];
int temp=0;
while (((temp=readercontent.read())!=-1)){
c[len] = (char)temp;
len++;
}
readercontent.close();
System.out.println();
System.out.printf("第四个读取内容为:"+new String(c,0,len));
}
}
文件的下载:
final File file = File.createTempFile(UUID.randomUUID().toString(), prefix);
saveData(sourceInputStream,file);
//前面两句是把网络流变成文件
//重点是后面这两句话
input = new FileInputStream(file);
bytes = IOUtils.toByteArray(input);
// 通过get请求得到读取器响应数据的数据流
public static InputStream getInputStreamByGet(String url) {
try {
HttpURLConnection conn = (HttpURLConnection) new URL(url)
.openConnection();
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inputStream = conn.getInputStream();
return inputStream;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// 将服务器响应的数据流存到本地文件
public static void saveData(InputStream is, File file) {
try (BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(file));) {
byte[] buffer = new byte[1024];
int len = -1;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
bos.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}