package com.atguigu.exer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
- Create a program named MyInput.java:
- Contain the methods for reading int, double, float, boolean, short,
- byte and String values from the keyboard
*/
public class MyInput {
public String nextString(){
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String str = null;
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt(){
return Integer.parseInt(nextString());
}
public boolean nextBoolean(){
return Boolean.parseBoolean(nextString());
}
public static void main(String[] args) {
MyInput i = new MyInput();
System.out.println("请输入一个字符串:");
String str = i.nextString();
System.out.println(str);
int j = i.nextInt();
System.out.println(j + 1);
}
}
package com.atguigu.exer;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.junit.Test;
public class TestExer {
//字符流复制 test.txt 为 test1.txt
@Test
public void test4(){
BufferedReader br = null;
BufferedWriter bw = null;
try{
br = new BufferedReader(new FileReader(new File(“test.txt”)));
bw = new BufferedWriter(new FileWriter(new File(“test2.txt”)));
char[] c = new char[20];
int len;
while((len = br.read(c)) != -1){
bw.write(c, 0, len);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(bw != null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(br != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//使用字符流实现内容的读入
@Test
public void test3(){
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("test.txt"));
String str;
while((str = br.readLine()) != null){
System.out.println(str);
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(br != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//使用字符流实现内容的输出
@Test
public void test2(){
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("test1.txt"));
String str = "Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,\n" +
"是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和\n" +
"Java平台(即JavaSE, JavaEE, JavaME)的总称。Java 技术具有卓" +
"越的通用性、高效性、平台移植性和安全性,广泛应用于个人PC、数据中心、游戏控制台、" +
"科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。在全球云计算" +
"和移动互联网的产业环境下,Java更具备了显著优势和广阔前景。";
bw.write(str);
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bw != null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 使用字节流实现内容的输出
@Test
public void test1() {
// FileOutputStream fos = new FileOutputStream(new File("test.txt"));
// BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(
new FileOutputStream(new File("test.txt")));
String str = "Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,\n" +
"是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和\n" +
"Java平台(即JavaSE, JavaEE, JavaME)的总称。Java 技术具有卓\n" +
"越的通用性、高效性、平台移植性和安全性,广泛应用于个人PC、数据中心、游戏控制台、\n" +
"科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。在全球云计算\n" +
"和移动互联网的产业环境下,Java更具备了显著优势和广阔前景。";
bos.write(str.getBytes());
bos.flush();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bos != null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package com.atguigu.exer;
//创建两个子线程,让其中一个输出1-100之间的偶数,另一个输出1-100之间的奇数。
class SubThread1 extends Thread{
public void run(){
for(int i = 1;i <= 100;i++){
if(i % 2 == 0){
System.out.println(Thread.currentThread().getName() + “:” + i);
}
}
}
}
class SubThread2 extends Thread{
public void run(){
for(int i = 1;i <= 100;i++){
if(i % 2 != 0){
System.out.println(Thread.currentThread().getName() + “:” + i);
}
}
}
}
public class TestThread {
public static void main(String[] args) {
SubThread1 st1 = new SubThread1();
SubThread2 st2 = new SubThread2();
st1.start();
st2.start();
//继承于Thread类的匿名类的对象
// new Thread(){
// public void run(){
// for(int i = 1;i <= 100;i++){
// if(i % 2 == 0){
// System.out.println(Thread.currentThread().getName() + “:” + i);
// }
// }
// }
// }.start();
//
// new Thread(){
// public void run(){
// for(int i = 1;i <= 100;i++){
// if(i % 2 != 0){
// System.out.println(Thread.currentThread().getName() + “:” + i);
// }
// }
// }
// }.start();
}
}
package com.atguigu.java;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.junit.Test;
public class TestObjectInputOutputStream {
// 对象的反序列化过程:将硬盘中的文件通过ObjectInputStream转换为相应的对象
@Test
public void testObjectInputStream() {
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(
“person.txt”));
Person p1 = (Person)ois.readObject();
System.out.println(p1);
Person p2 = (Person)ois.readObject();
System.out.println(p2);
}catch (Exception e) {
e.printStackTrace();
}finally{
if(ois != null){
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 对象的序列化过程:将内存中的对象通过ObjectOutputStream转换为二进制流,存储在硬盘文件中
@Test
public void testObjectOutputStream() {
Person p1 = new Person("小米", 23,new Pet("花花"));
Person p2 = new Person("红米", 21,new Pet("小花"));
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
oos.writeObject(p1);
oos.flush();
oos.writeObject(p2);
oos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
/*
- 要实现序列化的类: 1.要求此类是可序列化的:实现Serializable接口
- 2.要求类的属性同样的要实现Serializable接口
- 3.提供一个版本号:private static final long serialVersionUID
- 4.使用static或transient修饰的属性,不可实现序列化
*/
class Person implements Serializable {
private static final long serialVersionUID = 23425124521L;
static String name;
transient Integer age;
Pet pet;
public Person(String name, Integer age,Pet pet) {
this.name = name;
this.age = age;
this.pet = pet;
}
@Override
public String toString() {
return “Person [name=” + name + “, age=” + age + “, pet=” + pet + “]”;
}
}
class Pet implements Serializable{
String name;
public Pet(String name){
this.name = name;
}
@Override
public String toString() {
return “Pet [name=” + name + “]”;
}
}
package com.atguigu.java;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import org.junit.Test;
public class TestOtherStream {
@Test
public void testData1(){
DataInputStream dis = null;
try{
dis = new DataInputStream(new FileInputStream(new File(“data.txt”)));
// byte[] b = new byte[20];
// int len;
// while((len = dis.read(b)) != -1){
// System.out.println(new String(b,0,len));
// }
String str = dis.readUTF();
System.out.println(str);
boolean b = dis.readBoolean();
System.out.println(b);
long l = dis.readLong();
System.out.println(l);
}catch(Exception e){
e.printStackTrace();
}finally{
if(dis != null){
try {
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//数据流:用来处理基本数据类型、String、字节数组的数据:DataInputStream DataOutputStream
@Test
public void testData(){
DataOutputStream dos = null;
try {
FileOutputStream fos = new FileOutputStream("data.txt");
dos = new DataOutputStream(fos);
dos.writeUTF("我爱你,而你却不知道!");
dos.writeBoolean(true);
dos.writeLong(1432522344);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(dos != null){
try {
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 打印流:字节流:PrintStream 字符流:PrintWriter
@Test
public void printStreamWriter() {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("print.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 创建打印输出流,设置为自动刷新模式(写入换行符或字节 '\n' 时都会刷新输出缓冲区)
PrintStream 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(); // 换行
}
}
ps.close();
}
}
package com.atguigu.java;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.junit.Test;
/*
-
RandomAccessFile:支持随机访问
-
1.既可以充当一个输入流,有可以充当一个输出流
-
2.支持从文件的开头读取、写入
-
3.支持从任意位置的读取、写入(插入)
*/
public class TestRandomAccessFile {
//相较于test3,更通用
@Test
public void test4(){
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(new File(“hello1.txt”),“rw”);raf.seek(4); byte[] b = new byte[10]; int len; StringBuffer sb = new StringBuffer(); while((len = raf.read(b)) != -1){ sb.append(new String(b,0,len)); } raf.seek(4); raf.write("xy".getBytes()); raf.write(sb.toString().getBytes()); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(raf != null){ try { raf.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
}
//实现插入的效果:在d字符后面插入“xy”
@Test
public void test3(){
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(new File(“hello1.txt”),“rw”);raf.seek(4); String str = raf.readLine();//efg123456
// long l = raf.getFilePointer();
// System.out.println(l);
raf.seek(4);
raf.write("xy".getBytes());
raf.write(str.getBytes());
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(raf != null){
try {
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//实现的实际上是覆盖的效果
@Test
public void test2(){
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(new File("hello1.txt"),"rw");
raf.seek(4);
raf.write("xy".getBytes());
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(raf != null){
try {
raf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//进行文件的读、写
@Test
public void test1(){
RandomAccessFile raf1 = null;
RandomAccessFile raf2 = null;
try {
raf1 = new RandomAccessFile(new File("hello.txt"), "r");
raf2 = new RandomAccessFile(new File("hello1.txt"),"rw");
byte[] b = new byte[20];
int len;
while((len = raf1.read(b)) != -1){
raf2.write(b, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(raf2 != null){
try {
raf2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(raf1 != null){
try {
raf1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package com.atguigu.java1;
//单线程:主线程
public class TestMain {
public static void main(String[] args) {
method2(“atguigu.com”);
}
public static void method1(String str){
System.out.println(“method1…”);
System.out.println(str);
}
public static void method2(String str){
System.out.println(“method2…”);
method1(str);
}
}
package com.atguigu.java1;
/*
- 创建一个子线程,完成1-100之间自然数的输出。同样地,主线程执行同样的操作
- 创建多线程的第一种方式:继承java.lang.Thread类
*/
//1.创建一个继承于Thread的子类
class SubThread extends Thread{
//2.重写Thread类的run()方法.方法内实现此子线程要完成的功能
public void run(){
for(int i = 1;i <= 100;i++){
System.out.println(Thread.currentThread().getName() +":" + i);
}
}
}
public class TestThread {
public static void main(String[] args) {
//3.创建子类的对象
SubThread st1 = new SubThread();
SubThread st2 = new SubThread();
//4.调用线程的start():启动此线程;调用相应的run()方法
//一个线程只能够执行一次start()
//不能通过Thread实现类对象的run()去启动一个线程
st1.start();
//st.start();
//st.run();
st2.start();
for(int i = 1;i <= 100;i++){
System.out.println(Thread.currentThread().getName() +":" + i);
}
}
}
package com.atguigu.java1;
/*
- Thread的常用方法:
- 1.start():启动线程并执行相应的run()方法
- 2.run():子线程要执行的代码放入run()方法中
- 3.currentThread():静态的,调取当前的线程
- 4.getName():获取此线程的名字
- 5.setName():设置此线程的名字
- 6.yield():调用此方法的线程释放当前CPU的执行权
- 7.join():在A线程中调用B线程的join()方法,表示:当执行到此方法,A线程停止执行,直至B线程执行完毕,
- A线程再接着join()之后的代码执行
- 8.isAlive():判断当前线程是否还存活
- 9.sleep(long l):显式的让当前线程睡眠l毫秒
- 10.线程通信:wait() notify() notifyAll()
- 设置线程的优先级
- getPriority() :返回线程优先值
setPriority(int newPriority) :改变线程的优先级
*/
class SubThread1 extends Thread {
public void run() {
for (int i = 1; i <= 100; i++) {
// try {
// Thread.currentThread().sleep(1000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
System.out.println(Thread.currentThread().getName() + “:”
+ Thread.currentThread().getPriority() + “:” + i);
}
}
}
public class TestThread1 {
public static void main(String[] args) {
SubThread1 st1 = new SubThread1();
st1.setName("子线程1");
st1.setPriority(Thread.MAX_PRIORITY);
st1.start();
Thread.currentThread().setName("========主线程");
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() + ":"
+ Thread.currentThread().getPriority() + ":" + i);
// if(i % 10 == 0){
// Thread.currentThread().yield();
// }
// if(i == 20){
// try {
// st1.join();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
}
System.out.println(st1.isAlive());
}
}