java IO文件操作入门经典例子-学会这些例子,你会发现IO是如此简单
IO是JAVASE中非常重要的一块,是面向对象的完美体现,深入学习IO,你将可以领略到很多面向对象的思想。
在公司没活干,复习了一下IO,发现很多都忘记了,所以写的不好,只够初学用。我把我复习过程中写的代码贴出来,大家共同学习,并请多指教指教哈。顺便一起讨论IO
1、文件拷贝
try {
File inputFile = new File(args[0]);
if (!inputFile.exists()) {
System.out.println("源文件不存在,程序终止");
System.exit(1);
}
File outputFile = new File(args[1]);
InputStream in = new FileInputStream(inputFile);
OutputStream out = new FileOutputStream(outputFile);
byte date[] = new byte[1024];
int temp = 0;
while ((temp = in.read(date)) != -1) {
out.write(date);
}
in.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2、java读文件:实现统计某一目录下每个文件中出现的字母个数、数字个数、空格个数及行数,除此之外没有其他字符
String fileName = "D:/date.java.bak";
// String fileName = "D:/test.qqq";
String line;
int i = 0, j = 0, f = 0, k = 0;
try {
BufferedReader in = new BufferedReader(new FileReader(fileName));
line = in.readLine();
while (line != null) {
// System.out.println(line);
char c[] = line.toCharArray();
for (int i1 = 0; i1 < c.length; i1++) {
// 如果是字母
if (Character.isLetter(c[i1]))
i++;
// 如果是数字
else if (Character.isDigit(c[i1]))
j++;
// 是空格
else if (Character.isWhitespace(c[i1]))
f++;
}
line = in.readLine();
k++;
}
in.close();
System.out
.println("字母:" + i + ",数字:" + j + ",空格:" + f + ",行数:" + k);
} catch (IOException e) {
e.printStackTrace();
}
3、 从文件(d:\test.txt)中查出字符串”aa”出现的次数
try {
BufferedReader br = new BufferedReader(new FileReader(
"D:\\test.txt"));
StringBuilder sb = new StringBuilder();
while (true) {
String str = br.readLine();
if (str == null)
break;
sb.append(str);
}
Pattern p = Pattern.compile("aa");
Matcher m = p.matcher(sb);
int count = 0;
while (m.find()) {
count++;
}
System.out.println("\"aa\"一共出现了" + count + "次");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
4、 三种方法读取文件
try {
// 方法一
BufferedReader br = new BufferedReader(new FileReader(new File(
"D:\\1.xls")));
// StringBuilder bd = new StringBuilder();
StringBuffer bd = new StringBuffer();
while (true) {
String str = br.readLine();
if (str == null) {
break;
}
System.out.println(str);
bd.append(str);
}
br.close();
// System.out.println(bd.toString());
// 方法二
InputStream is = new FileInputStream(new File("d:\\1.xls"));
byte b[] = new byte[Integer.parseInt(new File("d:\\1.xls").length()
+ "")];
is.read(b);
System.out.write(b);
System.out.println();
is.close();
// 方法三
Reader r = new FileReader(new File("d:\\1.xls"));
char c[] = new char[(int) new File("d:\\1.xls").length()];
r.read(c);
String str = new String(c);
System.out.print(str);
r.close();
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
5、三种方法写文件
try {
PrintWriter pw = new PrintWriter(new FileWriter("d:\\1.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(
"d:\\1.txt")));
OutputStream os = new FileOutputStream(new File("d:\\1.txt"));
// 1
os.write("ffff".getBytes());
// 2
// bw.write("ddddddddddddddddddddddddd");
// 3
// pw.print("你好sssssssssssss");
bw.close();
pw.close();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
6、读取文件,并把读取的每一行存入double型数组中
try {
BufferedReader br = new BufferedReader(new FileReader(new File(
"d:\\2.txt")));
StringBuffer sb = new StringBuffer();
while (true) {
String str = br.readLine();
if (str == null) {
break;
}
sb.append(str + "、");
}
String str = sb.toString();
String s[] = str.split("、");
double d[] = new double[s.length];
for (int i = 0; i < s.length; i++) {
d[i] = Double.parseDouble(s[i]);
}
for (int i = 0; i < d.length; i++) {
System.out.println(d[i]);
}
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
------解决方案--------------------
给点小意见,其实IO流还是很简单的,只要我们将流和目标文件想象成管道和水桶就很容易理解和编程了!
不知道你是否看过那些顶级大师讲IO这章的视频,给人映像很深刻!
[解决办法]
我是初学者,觉得挺适合我的。
LZ,xiexie.....
[解决办法]
import java.io.*;
public class CopyBytes{
public static void main(String[]args)throw IOException{
File inputFile=new File("farrago.txt");
File outputFile=new File("outagainb.txt");
FileInputStream in=new FileInputStream(inutFile)
FileOututStream out=new FileOutputStream(outputFile);
int c;
while((c=in.read())!=-1)
out.write(c);
in.close();
out.close();
}
}
这是我在书上抄的文件复制的代码 楼主解释下 建立 byte date[]=new byte[1024]
和 date做 in.read()的参数吗
[解决办法]
不错,支持一下。
不过 IO 最好能在 finally 中 close 哦
[解决办法]
try {
File inputFile = new File(args[0]);
if (!inputFile.exists()) {
System.out.println("源文件不存在,程序终止");
System.exit(1);
}
File outputFile = new File(args[1]);
InputStream in = new FileInputStream(inputFile);
OutputStream out = new FileOutputStream(outputFile);
byte date[] = new byte[1024];
int temp = 0;
while ((temp = in.read(date)) != -1) {
out.write(date);
}
in.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
[解决办法]
读EXCEL的代码如下:
import java.io.*;
import jxl.*;
public class exercise
{
public static void main(String args[])
{
try
{
Workbook book=Workbook.getWorkbook(new File("D:\\1.xls"));
Sheet sheet=book.getSheet(0);
Cell cell1 = sheet.getCell(0, 0);
String result=cell1.getContents();
System.out.println(result);
book.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}