java IO文件操作简单基础入门例子,IO流其实没那么难
IO是JAVASE中非常重要的一块,是面向对象的完美体现,深入学习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();
}
java IO文件操作简单基础入门例子,IO流其实没那么难的更多相关文章
- Java IO,io,文件操作,删除文件,删除文件夹,获取文件父级目录
Java IO,io,文件操作,删除文件,删除文件夹,获取文件父级目录 这里先简单的贴下常用的方法: File.separator //当前系统文件分隔符 File.pathSeparator // ...
- java常见文件操作
收集整理的java常见文件操作,方便平时使用: //1.创建文件夹 //import java.io.*; File myFolderPath = new File(str1); try { if ( ...
- (Unity)XML文件读写与IO文件操作类使用介绍
using System.Xml; //xml文件操作命名空间 #region 写入操作 void WriteXMLFile(string _fileName) { Xm ...
- Java api 入门教程 之 JAVA的文件操作
I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程的一个基本 ...
- 【java】文件操作java.io.File
package 文件操作; import java.io.File; import java.io.IOException; public class TestFile { public static ...
- Java学习之==>IO文件操作体系
一.概述 在整个 Java.io 中最重要的就是5个类和一个接口.5个类指的是 File.InputStream.OutputStream.Reader.Writer,一个接口指的是Serializa ...
- 14、Java文件操作stream、File、IO
1.文件操作涉及到的基本概念 File File类 是文件操作的主要对象中文意义就是 文件 顾名思意 万物皆文件,在计算上看到的所有东西都是文件保存,不管是你的图片.视频.数据库数据等等都是按照基本的 ...
- Java 基本文件操作
Java 文件操作 , 这也是基于Java API 操作来实现的. 文件是操作系统管理外存数据管理的基本单位, 几乎所有的操作系统都有文件管理机制. 所谓文件, 是具有符号名而且在逻辑上具有完整意义的 ...
- java的文件操作类File
java.io.File类,是java获取文件/文件夹的所有属性,和完成所有相关操作的类 例子: package test.file.IO; import java.io.*; public clas ...
随机推荐
- stl_alloc.h分配器
五.分配器:5.1.头文件: 5.1.1.include<stl_alloc.h> //内存的分配. 5.1.2.include<stl_construct.h> //对象的构 ...
- 通过 jdbc 分析数据库中的表结构和主键外键
文章转自:http://ivan4126.blog.163.com/blog/static/20949109220137753214811/ 在某项目中用到了 hibernate ,大家都知道 hib ...
- mysql分页查询优化
由于MySql的分页机制:并不是跳过 offset 行,而是取 offset + N 行,然后返回放弃前 offset 行,返回N 行, 所以当 offset 特别大的时候,效率就非常的低下,要么控制 ...
- MySQL注入与防御(排版清晰内容有条理)
为何我要在题目中明确排版清晰以及内容有条理呢? 因为我在搜相关SQL注入的随笔博客的时候,看到好多好多都是页面超级混乱的.亲爱的园友们,日后不管写博客文章还是平时写的各类文章也要多个心眼,好好注意一下 ...
- IOS(二)基本控件UIButton、简易动画、transform属性、UIImageView
UIButton //1.设置UIButton 的左右移动 .center属性 获得 CGPoint 来修改x y //1.设置UIButton 的放大缩小 bounds属性 获得CGRect 然后通 ...
- .Net程序员学用Oracle系列(24):数据字典、死锁
1.静态数据字典 1.1.实用静态数据字典 1.2.运用静态数据字典 2.动态数据字典 2.1.实用动态性能视图 2.2.运用动态性能视图 3.死锁 3.1.定位死锁 3.2.解锁方法 3.3.强制删 ...
- Python基本语法--数据结构与运算符
# -*- coding: utf-8 -*- print "Hello, Python!"; print ("Hello, Python!"); #行和缩进 ...
- 安卓模拟器tools修改
defaults write com.apple.finder AppleShowAllFiles -bool true 这步是显示隐藏文件夹, 然后打开finder,在应用程序上右键,选择在上层文件 ...
- Java IO和NIO文章目录
1.java IO详尽解析 2.深入分析 Java I/O 的工作机制 3.InputStream类详解 4.OutputStream类详解 5.JAVA的节点流和处理流 6.FileInputStr ...
- React入门---组件-4
组件:网页可以分为多个模块,比如头部,底部,分享等各种模块,这些模块在其他页面也可能会用到,我们把这些分开,每一个模块当作一个组件,进行复用. 接下来直接以头部 header作为一个组件来进行demo ...