每天进步一点点
1.用输入输出流获取.txt文件上的字符数据。
程序清单:
package com.study.java;
import java.io.File; import java.io.FileInputStream; import java.io.IOException;
public class AnalyzeJasonOne {`
public static void main(String[] args) throws IOException {
String saveResult = new String();
final String path = "C:\\Users\\Lenovo\\Desktop\\jaon.txt";
//1、得到数据文件
File file = new File(path);
//2、建立数据通道
FileInputStream fileInputStream = new FileInputStream(file);
byte[] buf = new byte[1024];
int length = 0;
//循环读取文件内容,输入流中将最多buf.length个字节的数据读入一个buf数组中,返回类型是读取到的字节数。
//当文件读取到结尾时返回 -1,循环结束。
while((length = fileInputStream.read(buf)) != -1){
saveResult = new String(buf,0,length);
/*System.out.println(new String(buf,0,length));*/
System.out.println("\n"+saveResult);
}
/*System.out.println(buf.toString());*/
//最后记得,关闭流.
fileInputStream.close();
}
}
运行截图:
2.java实现多线程的三种常用方式(其实不止三种) (1)继承Tread类。 程序清单01: 描述:这个程序实现对于电脑来说是很容易的事情,所以你看起来像是顺序执行的,其实不然,当你把i的值变为1000时,就会发现其实线程是并发执行的,它们在争夺cpu资源。
package com.study.java.thread;
public class TestThread01 extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("我在看代码。");
}
}
public static void main(String[] args) {
TestThread01 testThread01 = new TestThread01();
for (int i = 0; i < 10; i++) {
System.out.println("我在学习多线程");
}
testThread01.run();
}
}
运行截图:
程序清单02:
package com.study.java.thread;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
//调用Thread方法,实现多线程同步下载图片
public class TestThread02 extends Thread{
private String url;//保存的图片地址
private String name;//保存的文件名
public TestThread02(String url,String name){
this.url = url;
this.name = name;
}
@Override
public void run() {
WebDownloader webDownloader = new WebDownloader();
webDownloader.downloader(url,name);
System.out.println("下载的文件名为"+name);
}
public static void main(String[] args) {
TestThread02 t1 = new TestThread02("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1697432864,1600199787&fm=26&gp=0.jpg", "1.jpg");
TestThread02 t2 = new TestThread02("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1546500353,2204894501&fm=26&gp=0.jpg", "2.jpg");
TestThread02 t3 = new TestThread02("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4015235454,569502415&fm=26&gp=0.jpg", "3.jpg");
t1.start();
t2.start();
t3.start();
}
}
//下载器
class WebDownloader{
//下载方法
public void downloader(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO异常,downloader出现问题");
}
}
}
运行截图:
(2)实现Rannable接口:
程序清单03:
package com.study.java.thread;
public class TestThread03 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("我在看代码。");
}
}
public static void main(String[] args) {
TestThread03 testThread03 = new TestThread03();
//Thread thread = new Thread(testThread03);
//thread.start();
new Thread(testThread03).start();
for (int i = 0; i < 10; i++) {
System.out.println("我在学习多线程");
}
}
}
运行截图:
程序清单04: 描述:为什么会出现-1的情况,那是因为没有加锁,现在还不会,唉。
package com.study.java.thread;
public class TestThread04 implements Runnable{
int ticket_number = 10;//定义票数
//重写run方法
@Override
public void run() {
while(true) {
if (ticket_number <= 0) {
break;
}
//模拟延时
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"-->拿到了第"+ticket_number--+"票");
}
}
public static void main(String[] args) {
TestThread04 testThread04 = new TestThread04();
new Thread(testThread04,"小明").start();
new Thread(testThread04,"老师").start();
new Thread(testThread04,"黄牛").start();
}
}
运行截图:
(3)实现Callable接口和开启Feture服务
程序清单:
package com.study.java.thread;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;
//线程创建方式3:
public class TestCallable implements Callable<Boolean> {
private String url;//保存的图片地址
private String name;//保存的文件名
public TestCallable(String url,String name){
this.url = url;
this.name = name;
}
@Override
public Boolean call() throws Exception {
WebDownloader webDownloader = new WebDownloader();
webDownloader.downloader(url,name);
System.out.println("下载的文件名为"+name);
return true;
}
public static void main(String[] args) {
TestCallable t1 = new TestCallable("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1697432864,1600199787&fm=26&gp=0.jpg", "1.jpg");
TestCallable t2 = new TestCallable("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1546500353,2204894501&fm=26&gp=0.jpg", "2.jpg");
TestCallable t3 = new TestCallable("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4015235454,569502415&fm=26&gp=0.jpg", "3.jpg");
//创建执行服务
ExecutorService ser = Executors.newFixedThreadPool(3);
//提交执行
Future<Boolean> r1 = ser.submit(t1);
Future<Boolean> r2 = ser.submit(t2);
Future<Boolean> r3 = ser.submit(t3);
//获取结果
try {
boolean rs1 = r1.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
//-------------------------
try {
boolean rs2 = r1.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
//------------------------
try {
boolean rs3 = r1.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
//关闭服务
ser.shutdown();
}
}
//下载器
class Downloader{
//下载方法
public void downloader(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO异常,downloader出现问题");
}
}
}
运行截图:
3 初始Lamada表达式
程序清单:
package com.study.java.thread;
public class Lamada {
public static void main(String[] args) {
Ilike ilike = (a) -> {
System.out.println("i like you " +a);
};
ilike.printlike(520);
}
}
interface Ilike {
public void printlike(int a);
}
运行截图:
4 打印九九乘法表
程序清单:
package com.study.java.example;
public class Dome05 {
public static void main(String[] args){
//打印九九乘法表
for(int i=1;i<=9;i++){
for(int j=1;j<=i ;j++){
System.out.print(j+"*"+i+"="+i*j+"\t");
}
System.out.println();
}
}
}
运行截图:
5 打印三角型
程序清单:
package com.study.java.example;
public class Demo07 {
public static void main(String[] args){
for(int i=1;i<=5;i++){
for(int j=5;j>=i;j--){
System.out.print(" ");
}
for(int j=1;j<=i;j++){
System.out.print("*");
}
for(int j=1;j<i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
运行截图: