练习1
使用System.in + BufferedReader + PrintWriter 实现键盘写入文件
输入quit 结束输入文件
字节流 转换到 字符流 inputStreamReader
public static void main(String[] args) throws IOException {
InputStream in = System.in;
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
PrintWriter pw = new PrintWriter(new File("/Users/lanou/Desktop/test3/kkk.txt"));
String string = null;
while((string = br.readLine()) != null) {
if(string.equals("quit")) {
break;
}
pw.write(string);
}
br.close();
pw.close();
}
导入外来包 调用里面的方法
包是别人建好的,里面封装了许多有用的方法,我们可以将此包导入,建立路径,然后使用里面的方法,
有助于我们编写代码
学习方法:
1.先找类 找对应的类(先看后缀)
2.看该类如何创建对象(获取对象)
3.看方法名 揣测方法的用意
4.测试看结果 然后整理在方法集中
commons-io 第三方源 jar包
commons-io-2.4-sources
commons-io-2.4.jar
获取路径扩展名 .txt .png
static String getExtension(String filename)
获取文件的名字
static String getName(String filename)
判断是不是这个扩展名
static boolean isExtension(String filename,String extension)
复制文件夹
static void copyDirectoryToDirectory(File src,File desc)
复制文件
static void copyFile(File src,File desc)
写入字符串到文件
static void writeStringToFile(File src,String date)
读取文件按字符串
static String readFileToString(File src)
写入文件 可以选取用什么字节流写入
static void write(String data, OutputStream output)
读取文件到集合中以字符串形式
static List<String> readLines(InputStream input)
代码示例:
public static void main(String[] args) throws IOException {
String extension = FilenameUtils.getExtension("/Users/lanou/Desktop/test3/kkk.txt");
System.out.println("扩展名:" + extension);
String name = FilenameUtils.getName("/Users/lanou/Desktop/test3/kkk.txt");
System.out.println("名字:" + name);
boolean b = FilenameUtils.isExtension("txt","/Users/lanou/Desktop/test3/kkk.txt");
System.out.println("是吗?:" + b);
FileUtils.copyDirectoryToDirectory(new File("/Users/lanou/Desktop/test3"), new File("/Users/lanou/Desktop/test3/fff"));
FileUtils.copyFile(new File("/Users/lanou/Desktop/test3/kkk.txt"), new File("/Users/lanou/Desktop/test3/kkk1111.txt"));
FileUtils.writeStringToFile(new File("/Users/lanou/Desktop/test3/kkk.txt"), "中国最伟大");
FileUtils.readFileToString(new File("/Users/lanou/Desktop/test3/kkk.txt"));
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/test3/kkk.txt");
IOUtils.write("我爱我的住过", fos);
List readLines = IOUtils.readLines(new FileInputStream(new File("/Users/lanou/Desktop/test3/kkk.txt")));
for (String string : readLines) {
System.out.println(string);
}
}
SequenceInputStream(Enumeration extends InputStream> e)
合并功能(可以把多个流读出(合并)一个流)
构造方法:
参数 是迭代器 是Vector特有的
该Vector要保存的是InputStream的子类
参数:传两个字节输入流
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/test3/1.txt");
File file1 = new File("/Users/lanou/Desktop/test3/2.txt");
File file2 = new File("/Users/lanou/Desktop/test3/1.txt");
FileInputStream fis = new FileInputStream(file);
FileInputStream fis1 = new FileInputStream(file1);
FileInputStream fis2 = new FileInputStream(file2);
Vector vector = new Vector<>();
vector.add(fis);
vector.add(fis1);
vector.add(fis2);
Enumeration enumeration = vector.elements();
SequenceInputStream sis = new SequenceInputStream(enumeration);
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/test3/5.txt");
int len = 0;
byte[] b = new byte[1024];
while((len = sis.read(b)) != -1) {
fos.write(b);
}
fos.close();
sis.close();
}
代码示例
1.将图片切割成多个1M的文件
2.然后将这些文件合并成一个图片
当程序中的某些图片或者视频不希望别人看到时,可以使用图片切割功能,编写代码,将6.9M的图片
分割为7个1M的文件,后缀名也可改为任意后缀名,保存在一个文件夹中,然后将原完整的图片删除,只保留这些碎片
当你想查看这些视频或者图片的时候,编写代码,进行图片连接,将这些图片连接成一个完整的图片,并改回后缀名,便可顺利查看
public static void main(String[] args) throws IOException {
FileInputStream fis = null;
Vector< FileInputStream> vector = new Vector<>();
for(int i = 1; i < 8; i++) {
String string = "/Users/lanou/Desktop/Test2/" + i + ".xxx";
File file = new File(string);
fis = new FileInputStream(file);
vector.add(fis);
}
Enumeration enumeration = vector.elements();
SequenceInputStream sis = new SequenceInputStream(enumeration);
FileOutputStream fos = new
FileOutputStream("/Users/lanou/Desktop/Test2/x.png");
int len = 0;
byte[] b = new byte[1024];
while((len = sis.read(b)) != -1) {
fos.write(b,0,len);
}
sis.close();
fos.close();
}
/**
* @throws FileNotFoundException
* @throws IOException
图片/视频切割
*/
public static void fun1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/test3/图1.png");
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] b = new byte[1024*1024];
int num = 1;
int len = 0;
BufferedOutputStream bos = null;
while((len = bis.read(b)) != -1) {
String string = "/Users/lanou/Desktop/Test2/" + num + ".xxx";
bos = new BufferedOutputStream(new FileOutputStream(string));
bos.write(b, 0, len);
num++;
bos.close();
}
bis.close();
}
线程
进程:一个正在运行的程序(独立运行的程序)
一个进程中可以只有一个线程 单线程程序
一个进程中也可以有多个线程 多线程程序
线程:一个线程 相当于一个cup的执行路径
(大大提升了处理效率)
标准的单线程程序
绝对安全 程序从上到下依次执行 但效率不高
public static void main(String[] args) {
add();
remove();
}
public static void add() {
for(int i = 0; i < 100; i++) {
System.out.println(i);
}
}
public static void remove() {
System.out.println("删除");
}
}
理解
--->--->
public static void main(String[] args) {
System.out.println(10/0);
add();
System.out.println("程序完成");
}
public static void add() {
for(int i = 0; i < 100; i++) {
System.out.println(i);
}
}
简单调用线程示例
package com.lanou3g.p03;
public class p11 {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
for (int i = 0; i < 30; i++) {
System.out.println("main--->" + i);
}
}
}
class MyThread extends Thread{
@Override
public void run() {
for(int i = 0; i < 30; i++) {
System.out.println("Thread--->" + i);
}
}
}
*创建Thread子类 NameThread
* 要求:
* 成员变量 String name set/get方法 重写run方法
* 要有(带名字的)构造方法 无参
package com.lanou3g.p03;
public class p12 {
}
class MyThread extends Thread{
private String mname;
public MyThread() {
}
public MyThread(String name,String myName) {
super(name);
this.mname = myName;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
@Override
public void run() {
super.run();
}
}
Runnable接口实现线程
package com.lanou3g.p03;
public class Demo13 {
public static void main(String[] args) {
MyRunable myRunable = new MyRunable();
Thread thread = new Thread(myRunable);
thread.start();
}
}
class MyRunable implements Runnable{
@Override
public void run() {
for(int i = 0; i < 20;i++) {
System.out.println(i);
}
}
}