1、昨日复习
1.如何遍历Map的key集,value集,key-value集,使用上泛型
Map
map.put();….
//遍历key
Set
for(String key : keySet){
System.out.println(key);
}
//遍历value
Collection
Iterator
while(iterator.hasNext()){
System.out.println(iterator.next());
}
//遍历key-value
Set
Iterator
while(iterator.hasNext()){
Map.Entry
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + “—->” + value);
}
- 写出使用Iterator 和 增强for 循环遍历List
的代码,使用上泛型
- 提供一个方法,用于遍历获取HashMap
中的所有value,并存放在List中返回。考虑上集合中泛型的使用。
public List
ArrayList
Collection
for(String value : values){
valueList.add(value);
}
return valueList;
}
- 创建一个与a.txt文件同目录下的另外一个文件b.txt
File file1 = new File(“d:\a\a.txt”);
File file2 = new File(file1.getParent(),”b.txt”);
- Map接口中的常用方法有哪些
增:put(K k,V v)
删: V remove(K k)
改: put(K k,V v)
查: V get(K k)
长度:int size()
遍历
2、File类的使用
package com.atguigu.java1;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
/**
* File类的使用
*
* 1、File类的一个对象,代表一个文件或一个文件目录(俗称:文件夹)
* 2、File类声明在java.io包下
* 3、File类中涉及到关于文件或文件目录的创建、删除、重命名、修改时间、文件大小等方法,
* 并未涉及到写入或读取文件内容的操作。如果需要读取或写入问文件内容,必须使用IO流来完成。
* 4、后续File类的对象常会作为参数传递到流的构造器中,指明读取或写入的“终点”。
*
*/
public class FileTest {
/*
1、如何创建File类的实例
public File(String pathname)
public File(String parent,String child)
public File(File parent,String child)
2、
相对路径:相较于某个路径下,指明的路径
绝对路径:包含盘符在内的文件或文件目录的路径
3、路径分隔符:
Windows:\\
Unix:/
*/
@Test
public void test1(){
//构造器1
File file1 = new File("hello.text");//相对于当前的module
File file2 = new File("D:\\IdeaProjects\\StudyJava\\Day25\\he.txt");
System.out.println(file1);
System.out.println(file2);
//构造器2
File file3 = new File("D:\\IdeaProjects\\StudyJava","JavaSenior");
System.out.println(file3);
//构造器3
File file4 = new File(file3,"hi.txt");
System.out.println(file4);
}
/*
public String getAbsolutePath():获取绝对路径
public String getPath() :获取路径
public String getName() :获取名称
public String getParent():获取上层文件目录路径。若无,返回null
public long length() :获取文件长度(即:字节数)。不能获取目录的长度。
public long lastModified() :获取最后一次的修改时间,毫秒值
如下的两个方法适用于文件目录
public String[] list() :获取指定目录下的所有文件或者文件目录的名称数组
public File[] listFiles() :获取指定目录下的所有文件或者文件目录的File数组
*/
@Test
public void test2(){
File file1 = new File("hello.txt");
File file2 = new File("D:\\io\\hi.txt");
System.out.println(file1.getAbsolutePath());
System.out.println(file1.getPath());
System.out.println(file1.getName());
System.out.println(file1.getParent());
System.out.println(file1.length());
System.out.println(file1.lastModified());
System.out.println("***********************");
System.out.println(file2.getAbsolutePath());
System.out.println(file2.getPath());
System.out.println(file2.getName());
System.out.println(file2.getParent());
System.out.println(file2.length());
System.out.println(file2.lastModified());
}
@Test
public void test3(){
File file = new File("D:\\IdeaProjects\\StudyJava");
String[] list = file.list();
for (String s : list){
System.out.println(s);
}
File[] files = file.listFiles();
for (File f:files){
System.out.println(f);
}
}
/*
public boolean renameTo(File dest):把文件重命名为指定的文件路径
比如file1.renameTo(file2)为例:
要想保证返回true,需要file1在硬盘中是存在的,且file2不能再硬盘中存在
*/
@Test
public void test4(){
File file1 = new File("hello.txt");
File file2 = new File("D:\\io\\hi.txt");
boolean renameTo = file2.renameTo(file1);
System.out.println(renameTo);
}
/*
public boolean isDirectory():判断是否是文件目录
public boolean isFile() :判断是否是文件
public boolean exists() :判断是否存在
public boolean canRead() :判断是否可读
public boolean canWrite() :判断是否可写
public boolean isHidden() :判断是否隐藏
*/
@Test
public void test5(){
File file1 = new File("hello.txt");
System.out.println(file1.isDirectory());
System.out.println(file1.isFile());
System.out.println(file1.exists());
System.out.println(file1.canRead());
System.out.println(file1.canWrite());
System.out.println(file1.isHidden());
}
/*
创建硬盘中对应的文件或文件目录
public boolean createNewFile() :创建文件。若文件存在,则不创建,返回false
public boolean mkdir() :创建文件目录。如果此文件目录存在,就不创建了。如果此文件目录的上层目录不存在,也不创建。
public boolean mkdirs() :创建文件目录。如果上层文件目录不存在,一并创建
public boolean delete():删除文件或者文件夹
Java中的删除不走回收站。 要删除一个文件目录,请注意该文件目录内不能包含文件或者文件目录
*/
@Test
public void test6() throws IOException {
//文件的创建
File file1 = new File("hi.txt");
if (!file1.exists()){
file1.createNewFile();
System.out.println("创建成功");
}else {
file1.delete();
System.out.println("删除成功");
}
}
@Test
public void test7(){
//文件目录的创建
File file1 = new File("D:\\io\\io1");
boolean mkdir = file1.mkdir();
if (mkdir){
System.out.println("创建成功");
}
File file2 = new File("D:\\io\\io2");
boolean mkdir1 = file2.mkdirs();
if (mkdir1){
System.out.println("创建成功2");
}
}
}
3、IO流原理及流的分类
I/O是Input/Output的缩写, I/O技术是非常实用的技术,用于处理设备之间的数据传输。
输入input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。
输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中。
文本数据:字符流;非文本数据:字节流
package com.atguigu.java1;
import org.junit.Test;
import java.io.*;
/*
一、流的分类:
1、操作数据单位:字节流、字符流
2、数据的流向:输入流、输出流
3、流的角色:节点流、处理流
二、流的体系结构
抽象基类 节点流 缓冲流
InputStream FileInputStream BufferedInputStream
OutputStream FileOutputStream BufferedOutputStream
Reader FileReader BufferedReader
Writer FileWriter BufferedWriter
*/
public class FileReadWriterTest {
/*
说明点:
1、read()的理解:返回读入的一个字符,如果达到文件末尾,返回-1
2、异常的处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理
3、读入的文件一定要存在,否则就会报异常
*/
@Test
public void test1() {
FileReader fr = null;
try {
//1、实例化File类的对象,指明要操作的对象
File file = new File("hello.txt");
//2、提供具体的流
fr = new FileReader(file);
//3、数据的读入
//read():返回读入的一个字符。如果达到文件末尾,返回-1
//方式一
/*int data = fr.read();
while (data!=-1){
System.out.print((char) data);
data = fr.read();
}*/
//方式二
int data;
while ((data = fr.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4、流的关闭操作
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//对read()操作升级,使用read重载方法
@Test
public void test2() {
FileReader fr = null;
try {
//1、File类的实例化
File file = new File("hello.txt");
//2、FileReader流的实例化
fr = new FileReader(file);
//3、读入的操作
//read(char[] cbuf):返回每次读入cbuf数组中的字符的个数,如果达到文件末尾,返回-1
char[] ch = new char[5];
int len;
while ((len = fr.read(ch)) != -1) {
for (int i = 0; i < len; i++) {
System.out.print(ch[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4、资源的关闭
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
从内存中写出数据到硬盘的文件里。
说明:
1、输出操作,对应的File可以不存在的。并不会报异常
2、
File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件
File对应的硬盘中的文件如果存在:
如果流使用的构造器是FileWriter(file,false)/FileWriter(file):对原有的文件的覆盖
如果流使用的构造器是FileWriter(file,true):不会对原有的文件的覆盖,而是在原有文件基础上追加的内容
*/
@Test
public void test3() throws IOException {
//1、提供File类的对象,指明写出到的文件
File file = new File("hello1.txt");
//2、提供FileWriter的对象,用于数据的写出
FileWriter fw = new FileWriter(file, false);
//3、写出的操作
fw.write("I have a dream!\n");
fw.write("you need to have a dream!");
//4、流的关闭
fw.close();
}
@Test
public void test4() {
FileReader fr = null;
FileWriter fw = null;
try {
// 1、创建File类的对象,指明读入和写出的文件
File srcFile = new File("hello.txt");
File destFile = new File("hello2.txt");
// 2、创建输入流和输出流的对象
fr = new FileReader(srcFile);
fw = new FileWriter(destFile);
// 3、数据的读入和写出操作
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1) {
fw.write(cbuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
// 4、关闭流资源
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
结论:
1、对于文本文件(.txt,.java,.c,.cpp),使用字符流处理,也可以用字节流来处理(但最好不要在控制输出台看)
2、对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,…),使用字节流处理
处理流之一:缓冲流的使用
1、缓冲流:
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
2、作用:提供流的读取、写入的速度
提高读写速度的原因:内部提供了一个缓冲区
3、处理流,就是“套接”在已有的流的基础上。
为了提高数据读写的速度,Java API提供了带缓冲功能的流类,在使用这些流类
时,会创建一个内部缓冲区数组,缺省使用8192个字节(8Kb)的缓冲区。
缓冲流要“套接”在相应的节点流之上,根据数据操作单位可以把缓冲流分为:
BufferedInputStream 和 BufferedOutputStream
BufferedReader 和 BufferedWriter
当读取数据时,数据按块读入缓冲区,其后的读操作则直接访问缓冲区
当使用BufferedInputStream读取字节文件时,BufferedInputStream会一次性从文件中读取8192个(8Kb),存在缓冲区中,直到缓冲区装满了,才重新从文件中读取下一个8192个字节数组。
向流中写入字节时,不会直接写到文件,先写到缓冲区中直到缓冲区写满,BufferedOutputStream才会把缓冲区中的数据一次性写到文件里。使用方法flush()可以强制将缓冲区的内容全部写入输出流
关闭流的顺序和打开流的顺序相反。只要关闭最外层流即可,关闭最外层流也会相应关闭内层节点流
flush()方法的使用:手动将buffer中内容写入文件
如果是带缓冲区的流对象的close()方法,不但会关闭流,还会在关闭流之前刷新缓冲区,关闭后不能再写出
BufferedReader br = null;
BufferedWriter bw = null;
try
{
// 创建缓冲流对象:它是处理流,是对节点流的包装
br = new BufferedReader(new FileReader("d:\\IOTest\\source.txt"));
bw = new BufferedWriter(new FileWriter("d:\\IOTest\\dest.txt"));
String str;
while ((str = br.readLine()) != null) { // 一次读取字符文本文件的一行字符
bw.write(str); // 一次写入一行字符串
bw.newLine(); // 写入行分隔符
}
bw.flush(); // 刷新缓冲区
} catch(
IOException e)
{
e.printStackTrace();
} finally
{
// 关闭IO流对象
try {
if (bw != null) {
bw.close(); // 关闭过滤流时,会自动关闭它所包装的底层节点流
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
package com.atguigu.java2;
/*
处理流之二:转换流的使用
1、转换流:
InputStreamReader:将一个字节的输入流转换为字符的输入流
OutputStreamWriter:将一个字符的输出流转换为字节的输出流
2、作用:提供字节流与字符流之间的转换
3、解码:字节、字节数组---->字符数组、字符串
编码:字符数组、字符串---->字节、字节数组
4、字符集
ASCII:美国标准信息交换码。
用一个字节的7位可以表示。
ISO8859-1:拉丁码表。欧洲码表
用一个字节的8位表示。
GB2312:中国的中文编码表。最多两个字节编码所有字符
GBK:中国的中文编码表升级,融合了更多的中文文字符号。最多两个字节编码
Unicode:国际标准码,融合了目前人类使用的所有字符。为每个字符分配唯一的字符码。所有的文字都用两个字节来表示。
UTF-8:变长的编码方式,可用1-4个字节来表示一个字符。
*/
import org.junit.Test;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class InputStreamReaderTest {
@Test
public void test1() throws IOException {
FileInputStream fis = new FileInputStream("dbcp.txt");
//参数2指明字符集,具体使用哪个字符集取决于文件保存时使用的字符集
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
char[] cbuf = new char[20];
int len;
while ((len = isr.read(cbuf)) !=-1){
String str = new String(cbuf,0,len);
System.out.print(str);
}
isr.close();
fis.close();
}
@Test
public void test2(){
File file1 = new File("dbcp.txt");
File file2 = new File("dbcp_gbk.txt");
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
isr = new InputStreamReader(fis,"UTF-8");
osw = new OutputStreamWriter(fos,"gbk");
char[] ch = new char[20];
int len;
while ((len = isr.read(ch))!=-1){
osw.write(ch,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.atguigu.java2;
import org.junit.Test;
import java.io.*;
import java.util.Locale;
import java.util.Scanner;
/*
其他流的使用:
1、标准的输入、输出流
2、打印流
3、数据流
*/
public class OtherStreamTest {
/*
1、标准的输入、输出流
1.1
System.in:标准的输入流,默认从键盘输入
System.out:标准的输出流,默认从控制台输出
1.2
System类的setIn(InputStream is)/setOut(PrintStream out) 方式重新指定输入和输出的流。
1.3练习
从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作,
直至当输入“e”或者“exit”时,退出程序。
方法一:使用Scanner实现,调用next()返回一个字符串
方法二:使用System.in实现。System.in--->转换流--->BufferedReader的readLine()方法
*/
public static void main(String[] args) {
BufferedReader br = null;
try {
InputStreamReader isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
while (true){
System.out.println("请输入字符串:");
String data = br.readLine();
if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)){
System.out.println("程序结束");
break;
}
String upperCase = data.toUpperCase();
System.out.println(upperCase);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
2、打印流:PrintStream和PrintWriter实现将基本数据类型的数据格式转化为字符串输出
2.1、提供了一系列重载的print()和println()
2.2、练习:
*/
@Test
public void test2(){
PrintStream ps = null;
try {
FileOutputStream fos = new FileOutputStream(new File("D:\\IO\\text.txt"));
// 创建打印输出流,设置为自动刷新模式(写入换行符或字节 '\n' 时都会刷新输出缓冲区)
ps = new PrintStream(fos, true);
if (ps != null) {// 把标准输出流(控制台输出)改成文件
System.setOut(ps);
}
for (int i = 0; i <= 255; i++) { // 输出ASCII字符
System.out.print((char) i);
if (i % 50 == 0) { // 每50个数据一行
System.out.println(); // 换行
} }
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (ps != null) {
ps.close();
} }
}
/*
3、数据流
3.1 DataInputStream 和 DataOutputStream
3.2 用于读取和写出基本数据类型、String类的数据
*/
}