java中io流代码 javaio流有什么用

java中的io流有哪些Java中的IO流
指的是将不同的输入输出源通过流的形式进行输入或输出的操作,流是一种抽象的描述 , 在程序中指的是数据的一种转移方式 。
IO流的分类:
(1)按照数据的流向:
输入流、输出流
(2)按照流数据的格式:
字符流、字节流
(3)按照流数据的包装过程:
节点流(低级流)、处理流(高级流)
最基本的几种进行简单介绍:
?InputStream/Reader: 所有的输入流的基类 , 前者是字节输入流,后者是字符输入流 。
?OutputStream/Writer: 所有输出流的基类,前者是字节输出流 , 后者是字符输出流 。
java IO流如果in==null , 就说明in这个引用所指向的对象还没有被创建,自然in.close()也就不可能被执行,而且会报异常 。我是这样想的,程序中如果你先声明变量InputStream(或者其他流类型)in=null;然后执行其他语句,再in=new、、、、在innew之前其他语句出exception,这时流没有被打开,也就无需关闭,如果不判断if(null!=in)硬要关闭in就出exception另外你的代码按如下写会更合理:if(null!=in){try{in.flush();in.close();in=null;}catch(IOExectione){};}
java io流不知道你是想做什么java中io流代码的,不过你的EOF很容易解释,你只写java中io流代码了一行,你却读java中io流代码了2次
看这3行
b.writeUTF("SayString \"");
System.out.println(a.readUTF());
temp = (String)a.readUTF();
具体要根据你自己的情况判断把哪一行给删除了
package test;
import java.io.*;
public class Test2 {
public static void main(String[] args) throws IOException {
// String temp = new String();
String temp = new String();
try {
FileInputStream in = new FileInputStream("d:\\a.txt");
DataInputStream a = new DataInputStream(in);
FileOutputStream out = new FileOutputStream("d:\\b.txt");
DataOutputStream b = new DataOutputStream(out);
do {
b.writeUTF("SayString \"");
System.out.println(a.readUTF());
//temp = (String) a.readUTF();
// temp="wahaha";
b.writeUTF(temp);
b.writeUTF("\"");
b.writeUTF("\n");
b.writeUTF("KeyPress 13,1");
b.writeUTF("\n");
b.writeUTF("Delay 700");
b.writeUTF("\n");
} while (temp == "end");
b.close();
a.close();
out.close();
in.close();
} catch (IOException e) {
System.out.println("ERROR");
}
}
}
java 中IO流操作字符串 两个文件中有一些相同的代码 现在要将两个合并成一个 并且去除相同代码 祥看补充你看看这个 , 是不是符合你的要求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class TestOne {
/**
* @param args
*/
public static void main(String[] args) {
String filepath1 = "F:\\1.txt";
String filepath2 = "F:\\2.txt";
ListString res1txt = null;
ListString res2txt = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//输入连续的个数
System.out.print("Input series string size:");
try {
int temp = Integer.parseInt(br.readLine());
res1txt = readFile(filepath1, temp);
res2txt = readFile(filepath2, temp);
//比较两个list中的相同值
for (int i = 0; ires1txt.size(); i) {
for (int j = 0; jres2txt.size(); j) {
if(res1txt.get(i).equals(res2txt.get(j))){
System.out.println(res1txt.get(i));
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 读取文件,把结果以输入的连续个数分割,并保存在List中
* @param filepath
* @param size
* @return
*/
public static ListString readFile(String filepath, int size){
//如果size不合法,则返回空
if(size = 0)
return null;
ListString list = new ArrayListString();
BufferedReader br = null;
try {
//读文件
br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath)));
String temp = null;
while((temp = br.readLine()) != null){
for (int i = 0; itemp.length() - size; i) {
list.add(temp.substring(i, i size));
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
}
这里发不了代码,只能以文本的形式了
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class TestOne {
/**
* @param args
*/
public static void main(String[] args) {
String filepath1 = "F:\\1.txt";
String filepath2 = "F:\\2.txt";
ListString res1txt = null;
ListString res2txt = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//输入连续的个数
System.out.print("Input series string size:");
try {
int temp = Integer.parseInt(br.readLine());
res1txt = readFile(filepath1, temp);
res2txt = readFile(filepath2, temp);
//比较两个list中的相同值
for (int i = 0; ires1txt.size(); i) {
for (int j = 0; jres2txt.size(); j) {
if(res1txt.get(i).equals(res2txt.get(j))){
System.out.println(res1txt.get(i));
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 读取文件,把结果以输入的连续个数分割,并保存在List中
* @param filepath
* @param size
* @return
*/
public static ListString readFile(String filepath, int size){
//如果size不合法,则返回空
if(size = 0)
return null;
ListString list = new ArrayListString();
BufferedReader br = null;
try {
//读文件
br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath)));
String temp = null;
while((temp = br.readLine()) != null){
for (int i = 0; itemp.length() - size; i) {
list.add(temp.substring(i, i size));
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
}
利用Java的IO流进行操作复制 , 代码如下,不知道为什么出现了错误java中io流代码我今天正好写这里,贴给java中io流代码你看看
file1写给file2
FileInputStream is= new FileInputStream(file1);
OutputStream os = new FileOutputStream(file2);
int read = 0;
byte[] buffer = new byte[8096];
while ((read = is.read(buffer))0) {
os.write(buffer, 0, read);
}
【java中io流代码 javaio流有什么用】关于java中io流代码和javaio流有什么用的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

    推荐阅读