笔记-迎难而上之Java基础进阶6
import java.io.*;
public class InputStreamDemo{
public static void main(String[] args) throws IOException{
//创建输入流对象,创建对象时绑定读取的数据源
FileInputStream fis = new FileInputStream("E:\\study\\demo12\\c.txt");
//创建byte[] 数组,让他一次性读取多个字节
byte[] bytes = new byte[1024];
int len=0;
//用len来接收读取到的有效字节
//因为文件中有5个字节,所以把五个字节传递给len
while((len=fis.read(bytes))!=-1){
//String的构造方法,可以把字节数组变成字符串
System.out.println(new String(bytes));
}
fis.close();
}
}
文件复制
package demo16;
import java.io.*;
public class CopyDemo{
public static void main(String[] args) throws IOException{
//创建FileInputStream对象,需要指定读取的数据源文件的位置
FileInputStream fis = new FileInputStream("C:\\CloudMusic\\MV\\周二珂 - 告白气球.mp3");
//绑定要输出的的数据目的地
FileOutputStream fos = new FileOutputStream("E:\\study\\demo13\\周二珂 - 告白气球.mp3");
//创建字节数组,让他一次性读取多个文件
byte[] bytes = new byte[1024];
//创建一个整数变量,让他接收读取到的有效字节和写入的有效字节
int date = 0;
//当读取到文件末尾时会返回-1,只要读取的字节不等于-1,就继续读取
//把读取到的有效字节,赋值给date,只要不等于-1,就写入文件
//从输入流中读取一些字节数并将它们存储到缓冲区数组bytes中。
while((date=fis.read(bytes))!=-1){
//读一个字节写一个字节
//为什么写入的数据是bytes??,因为读取的数据就存储到bytes中
fos.write(bytes,0,date);
}
fos.close();
fis.close();
}
}
字符输入流Reader类
Reader类是字符流最顶层的类
成员方法:
- int read() 读取单个字符并返回
- int read(char[] cubf)一次性读取多个字符,将字符读入数组
- void close() 关闭流并释放资源
FileReader继承了Reader
构造方法 FileReader(String fileName) 参数是一个文件的路径
FileReader(File file) 参数是一个文件
字符输入流读取字符数据
import java.io.*;
public class ReaderDemo{
public static void main(String[] args) throws IOException{
//字符流的创建,需要绑定数据源
//绑定了文件路径,也可以绑定文件
FileReader fr = new FileReader("E:\\study\\demo13\\a.txt");
//使用循环来读取文件
/*int len = 0;
//read(),返回的是读取到的字符
while((len=fr.read())!=-1){
System.out.println((char)len);
}*/
int len = 0;
//试一下用数组来读取文件
//定义一个字符数组,他是用来存储读取到的字符
char[] cs = new char[1024];
while((len=fr.read(cs))!=-1){
//String的构造方法,可以把字符数组转换成字符串
System.out.println(new String(cs,0,len));
}
fr.close();
}
}
字符输出流的基本使用_写单个字符到文件
public static void main(String[] args) throws IOException{
//创建FileWriter对象,绑定写入的数据目的地
//如果没有文件会创建文件
FileWriter fw = new FileWriter("E:\\study\\demo13\\b.txt");
//write(int c)写入单个字符,
fw.write(97);//这里只是把数据写入到内存缓冲区中
//需要flush()把存在内存缓冲区的数据写入到文件中
fw.flush();
fw.close();
}
flush方法和close方法的区别
flush:刷新缓冲区,流对象可以继续使用
close:先刷新缓冲区,通知系统释放资源,流对象不能再使用
字符输出流写数据的其他方法
import java.io.*;
public class WriterDemo1{
public static void main(String[] args) throws IOException{
FileWriter fw = new FileWriter("E:\\study\\demo13\\d.txt");
//void write(char[] cbuf)可以写入字符数组
char[] cs = {'云','想','衣','裳','花','想','容'};
fw.write(cs);
//写入字符数组的一部分,从索引2开始写,写四个字符
fw.write(cs,2,4);
//写字符串
fw.write("春风扶槛露华浓");
//写一部分字符串
fw.write("若非群玉山头见",0,3);
fw.close();
}
}
字符输出流的续写和换行
public class WriteDemo2{
public static void main(String[] args){
//如若参数为false,则会覆盖源文件,为true会在源文件的后面续写
FileWriter fw = new FileWriter("E:\\study\\demo13\\f.txt",true);
fw.write("云想衣裳花想容");
//换行符是\r\n
fw.write("\r\n"+"春风扶槛露华浓");
fw.write("\r\n"+"若非群玉山头见");
fw.write("\r\n"+"会向瑶台月下逢");
fw.close();
//现在仿佛执行程序都不会覆盖源文件
}
}
Properties集合存储数据
Properties类表示了一个持久的属性集,properties 可保存在流中或从流中加载
Properties集合是一个唯一和IO流相结合的集合
//PropertiesDemo集合的常见方法和使用
import java.util.*;
public class PropertiesDemo{
public static void main(String[] args){
//创建Properties集合的对象
Properties prop = new Properties();
//调用Properties的方法setProperty
prop.setProperty("李白","18");
prop.setProperty("杜甫","22");
prop.setProperty("苏轼","25");
//取出键
Set<String> set = prop.stringPropertyNames();
//遍历Set,通过K获取v
for(String key : set){
String value = prop.getProperty(key);
System.out.println(key+" "+value);
}
}
}
Properties集合中的方法store
void store(OutputStream out, String comments),字节输出流,不能写入中文
void store(Writer writer,String comments)字符输出流,可以写中文
import java.util.*;
import java.io.*;
public class PropertiesDemo{
public static void main(String[] args) throws IOException{
//创建Properties集合的对象
Properties prop = new Properties();
//调用Properties的方法setProperty
prop.setProperty("李白","18");
prop.setProperty("杜甫","22");
prop.setProperty("苏轼","25");
//Properties集合中的方法store,可以把集合中的临时数据写入到硬盘中去
FileWriter fw = new FileWriter("E:\\study\\demo13\\e.txt");
prop.store(fw,"save date");
fw.close();
}
}
Properties集合中的方法load
可以使用Properties集合中的方法load,把硬盘中保存的文件,读取到集合中使用
void load(InputStream inStream)字节输入流,不能读取中文的键值对
void load(Reader reader)字符输入流,能读取含有中文的键值对
public static void main(String[] args) throws IOException{
Properties prop = new Properties();
prop.load(new FileReader("E:\\study\\demo13\\e.txt"));
Set<String> set = prop.stringPropertyNames();
for(String key :set){
String value = prop.getProperty(key);
System.out.println(key+"="+value);
}
}
BufferedOutputStream_字节缓冲输出流
BufferedOutputStream继承自OutputStream
BufferedOutputStream的构造方法:
BufferedOutputStream(OutputStream out):创建一个新的缓冲输入流,已将数据写入指定的底层输出流
BufferedOutputStream(OutputStream out,int size):创建一个新的缓冲输入流,已将数据写入指定的底层输出流,并且指定缓冲区的大小
import java.io.*;
public class BufferedOutputStreamDemo {
public static void main(String[] args) throws IOException{
FileOutputStream fos = new FileOutputStream("E:\\study\\demo13\\g.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("云想衣裳花想容".getBytes());
bos.close();
}
}
BufferedInputStream_字节缓冲输入流
import java.io.*;
public class BufferedInputStreamDemo {
public static void main(String[] args) throws IOException{
FileInputStream fis = new FileInputStream("E:\\study\\demo13\\g.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
int len=0;
while((len=bis.read())!=-1){
System.out.println((char)len);
}
bis.close();
}
}
BufferedWriter_字符缓冲输出流
//这段代码在编译器不报错,在命令行一直报错
public static void main(String[] args) throws IOException{
//创建字符缓冲输出流
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\study\\demo13\\h.txt"));
//调用方法写入字符
for(int i=0;i<10;i++){
bw.write("云想衣裳花想容");
bw.newLine();
}
//关闭流,同时flush把缓冲区的数据,刷新到文件中
bw.close();
}
BufferedReader_字符缓冲输入流
public static void main(String[] agrs) throws IOException{
//创建一个BufferedReader对象
BufferedReader br = new BufferedReader(new FileReader("E:\\study\\demo13\\h.txt"));
//调用方法read或者readLine,readLine返回的是一行,没有就返回null
String line;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
}
文本排序练习
import java.io.*;
import java.util.HashMap;
//把一个打乱序号的文本恢复正常的排序
public class CopyDemo{
public static void main(String[] args) throws IOException{
//分别存储序号和文本
HashMap<String,String> hm = new HashMap<>();
BufferedReader br = new BufferedReader(new FileReader("E:\\study\\demo13\\k.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\study\\demo12\\k.txt"));
String line;
while((line=br.readLine())!=null){
//将字符串进行切割,返回的是一个字符串数组,分别有索引0和索引1
//可以把0为序号,1为文本内容
//调用字符串的切割文本方法
String[] arr = line.split("\\.");
//key值会自动排序
hm.put(arr[0],arr[1]);
}
//keySet会把HashMap的键全部取出来,放到一个集合中
//遍历这个集合就可以拿到key
for(String key : hm.keySet()){
String value=hm.get(key);
line = key +"."+value;
//读一行,写一行
bw.write(line);
bw.newLine();
}
bw.close();
br.close();
}
}
转换流
OutputStreamWriter:
构造方法
OutputStreamWriter(OutputStream out)
OutputFtreamWriter(OutputStream out, string CharseName)
参数OutputStream:字节输出流,可以用来写转换之后的字节到文件中
参数String charsetName:编码名称
使用步骤:
创建OutputStreamWrite对象,构造方法中传递字节输出流和编码名称
使用OutputStreamWriter对象中的方法write,把字符转换为字节存储缓冲区中
使用OutputStreamWriter对象中的方法flush
import java.io.*;
public class OutputStreamWriterDemo{
public static void main(String[] args) throws IOException{
GBK_write();
}
public static void utf_8_write() throws IOException{ //传递一个字节输出流的对象,默认编码表是utf-8
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("E:\\study\\demo13\\i.txt"));
osw.write("云想衣裳花想容");
osw.flush();
osw.close();
}
public static void GBK_write() throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("E:\\study\\demo13\\J.txt"),"GBK");
osw.write("春风扶槛露华浓");
osw.flush();
osw.close();
}
}InputStreamReader类
可以按照指定的编码表来解码
构造方法:
InputStreamReader(InputStream in)
InputStreamReader(InputStream in,String charsetName)
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputStreamReaderDemo {
public static void main(String[] args) throws IOException {
//read_utf_8();
read_GBK();
}
private static void read_GBK() throws IOException {
//读取GBK文件
InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\\study\\demo13\\J.txt"),"GBK");
int len=0;
while ((len=isr.read())!=-1){
System.out.println((char)len);
}
isr.close();
}
private static void read_utf_8() throws IOException {
//读取utf-8文件
InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\\study\\demo13\\i.txt"));
int len=0;
while ((len=isr.read())!=-1){
System.out.println((char)len);
}
isr.close();
}
}
笔记-迎难而上之Java基础进阶6的更多相关文章
- 笔记-迎难而上之Java基础进阶1
Collection集合 数组的长度是固定的,集合的长度是可变的 数组中存储的是同一类型的元素,可以存储基本数据类型值.集合存储的都是对象.而且对象的类型可以不一致. 集合框架 import java ...
- 笔记-迎难而上之Java基础进阶-终
使用Stream流的方式,遍历集合 import java.util.*; public class StreamDemo{ public static void main(String[] args ...
- 笔记-迎难而上之Java基础进阶8
函数式接口 函数式接口:有且只有一个抽象方法的接口,(可以包含其他默认,静态,私有方法) //函数式接口的使用,创建一个函数式接口 public interface FunctionalInterfa ...
- 笔记-迎难而上之Java基础进阶7
序列化流 把对象以流的方式写入到文件中保存,叫做对象的序列化 把文件中保存的对象,以流的方式读取出来,叫做对象大反序列化 对象的序列化流_ObjectOutputtream继承自OutputStrea ...
- 笔记-迎难而上之Java基础进阶2
Set集合 import java.util.*; public class HashSetDemo{ public static void main(String[] args){ //Set接口的 ...
- 笔记-迎难而上之Java基础进阶3
统计字符串中每一个不同的字符 import java.util.*; //统计字符串每一个字符出现的字数 public class StringDemo{ public static void mai ...
- 笔记-迎难而上之Java基础进阶5
Lambda表达式无参数无返回值的练习 //定义一个接口 public interface Cook{ public abstract void makeFood(); } public class ...
- 笔记-迎难而上之Java基础进阶4
内部类创建多线程 //匿名内部类创建多线程 public class InnerClassThread{ public static void main(String[] args){ //回忆一下之 ...
- 第二十八节:Java基础-进阶继承,抽象类,接口
前言 Java基础-进阶继承,抽象类,接口 进阶继承 class Stu { int age = 1; } class Stuo extends Stu { int agee = 2; } class ...
随机推荐
- 面试之mybatis和hibernate的区别
mybatis是支持普通SQL查询.存储过程和高级映射的优秀持久层框架.封装了 几乎所有的JDBC代码和参数的手工设置 ,以及结果集的检索: 封装了:1,获取连接,执行sql,释放连接. 2,sql的 ...
- Statues CodeForces - 129C(bfs)
In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the oppo ...
- 线段树:CDOJ1592-An easy problem B (线段树的区间合并)
An easy problem B Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...
- python - work4
# -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: work_20181108.py@ide: PyCharm Communit ...
- 常见python快捷键
http://www.cnblogs.com/toutou/p/4778818.html Ctrl+/注释(取消注释)选择的行 Shift + Enter开始新行 Ctrl + Enter智能换行 T ...
- [python学习篇][书籍学习][python standrad library][内置类型]对象测试真值,布尔值操作, 比较操作
几乎所有对象都可以比较.测试真值.转换为字符串(其实就是用repr()函数,或略有差异的str()函数来转换) 1 对象是否为真 任何对象都可以测试真值,用于if或while的条件或下面布尔运算的操作 ...
- PHP变量的生命周期
变量不仅有其特定的作用范围,还有其存活的周期--生命周期.变量的生命周期指的是变量可被使用的一个时间段,在这个时间段内变量是有效的,一旦超出这个时间段变量就会失效,我们就不能够再访问到该变量的值了. ...
- Welcome-to-Swift-24高级运算符(Advanced Operators)
除了基本操作符中所讲的运算符,Swift还有许多复杂的高级运算符,包括了C语和Objective-C中的位运算符和移位运算. 不同于C语言中的数值计算,Swift的数值计算默认是不可溢出的.溢出行为会 ...
- kb-07线段树--10--dfs序建树
/* hdu3974 dfs序建树,然后区间修改查询: */ #include<iostream> #include<cstdio> #include<cstring&g ...
- poj1324 Holedox Moving
Holedox Moving Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 16980 Accepted: 4039 D ...