1、向array.txt文件中写入如下内容:(集合到文件) lin,30 zhang,20 hh,25
package learnjava;
import java.io.*;
import java.util.*;
public class Demo {
public static Set<String> res=new HashSet<>();
public static void main(String[] args) throws IOException {
ArrayList<Student> array=new ArrayList<>();
Student s1=new Student("lin",30);
Student s2=new Student("zhang",20);
Student s3=new Student("hh",25);
array.add(s1);
array.add(s2);
array.add(s3);
BufferedWriter bw=new BufferedWriter(new FileWriter("hh\\array.txt"));
for(Student s:array){
bw.write(s.getName()+","+s.getAge());
bw.newLine();
}
bw.close();
}
}
2、复制多级文件夹
package learnjava;
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException{
File from=new File("C:\\数据库作业");
File to=new File("D:\\");
copyFolder(from,to);
}
public static void copyFolder(File from,File to) throws IOException{
if(from.isDirectory()){
File fromCopy=new File(to,from.getName());
if(!fromCopy.exists()){
fromCopy.mkdir();
}
File[] fileArray=from.listFiles();
for(File file:fileArray){
copyFolder(file,fromCopy);
}
}else{
File newFile=new File(to,from.getName());
copyFile(from,newFile);
}
}
public static void copyFile(File srcFile,File destFile) throws IOException{
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bys=new byte[1024];
int len;
while ((len=bis.read(bys))!=-1){
bos.write(bys,0,len);
}
bis.close();
bos.close();
}
}
3、复制文件的异常处理:
(1)
public static void method() {
FileReader fr=null;
FileWriter fw=null;
try{
fr=new FileReader("fr.txt");
fw=new FileWriter("fw.txt");
char[] chs=new char[1024];
int len;
while ((len=fr.read(chs))!=-1) {
fw.write(chs, 0, len);
}
}catch(IOException e){
e.printStackTrace();
}finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
(2)JDK7版本改进
public static void method() {
try(FileReader fr=new FileReader("fr.txt");
FileWriter fw=new FileWriter("fw.txt");){
char[] chs=new char[1024];
int len;
while ((len=fr.read(chs))!=-1) {
fw.write(chs, 0, len);
}
}catch(IOException e){
e.printStackTrace();
}
}
4、对象序列化流、反序列化流(seriaVersionID、transient)
package learnjava;
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private transient int age;
public Student(){
}
public Student(String name,int age){
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package learnjava;
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
write();
read();
}
public static void write() throws IOException {
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("hh\\oos.txt"));
Student s=new Student("林青霞",30);
oos.writeObject(s);
oos.close();
}
public static void read() throws IOException, ClassNotFoundException {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("hh\\oos.txt"));
Object s=ois.readObject();
Student stu=(Student) s;
System.out.println(stu.getName()+","+((Student) s).getAge());
ois.close();
}
}
5、游戏次数案例
package learn;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class Demo {
public static void main(String[] args) throws IOException {
Properties prop=new Properties();
FileReader fr=new FileReader("D:\\game.txt");
prop.load(fr);
fr.close();
int count=Integer.parseInt(prop.getProperty("count"));
if(count>=3){
System.out.println("游戏试玩已结束,想玩请充值(www.itcast.cn)");
}else{
GuessNumber.start();
count++;
prop.setProperty("count",String.valueOf(count));
FileWriter fw=new FileWriter("D:\\game.txt");
prop.store(fw,null);
fw.close();
}
}
}
6、买票案例:
package learn;
public class SellTicket implements Runnable {
private int tickets=100;
@Override
public void run(){
while (true){
if(tickets>0){
System.out.println(Thread.currentThread().getName()+"正在出售第"+
tickets+"张票");
tickets--;
}
}
}
}
package learn;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException {
SellTicket st=new SellTicket();
Thread t1=new Thread(st,"窗口1");
Thread t2=new Thread(st,"窗口2");
Thread t3=new Thread(st,"窗口3");
t1.start();
t2.start();
t3.start();
}
}
同步方法解决数据安全问题:
package learn;
public class SellTicket implements Runnable {
private int tickets=100;
@Override
public void run(){
while (true){
sellTickets();
}
}
public synchronized void sellTickets(){
if(tickets>0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"正在出售第"+
tickets+"张票");
tickets--;
}
}
}
7、线程安全的类: StringBuffer Vector Hashtable
Collections中的synchronizedList、synchronizedMap、synchronizedSet方法
public static <T> List<T> synchronizedList?(List<T> list)返回由指定列表支持的同步(线程安全)列表。
public static <K,V> Map<K,V> synchronizedMap?(Map<K,V> m)返回由指定地图支持的同步(线程安全)映射。
public static <T> Set<T> synchronizedSet?(Set<T> s)返回由指定集合支持的同步(线程安全)集。
List<String> list= Collections.synchronizedList(new ArrayList<>());
8、案例:生产者生产一瓶牛奶,消费者消费一瓶
Class IllegalMonitorStateException 抛出以表示线程已尝试在对象的监视器上等待或通知其他线程等待对象的监视器,而不拥有指定的监视器。
package learn;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException {
Box b=new Box();
Producer p=new Producer(b);
Consumer c=new Consumer(b);
Thread t1=new Thread(p);
Thread t2=new Thread(c);
t1.start();
t2.start();
}
}
package learn;
public class Box {
private int milk;
private boolean state;
public synchronized void put(){
if(state){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
milk++;
System.out.println("生产者将第"+milk+"瓶奶放入奶箱");
state=true;
notifyAll();
}
public synchronized void get(){
if(!state){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("消费者拿到第"+milk+"瓶奶");
state=false;
notifyAll();
}
}
package learn;
public class Producer implements Runnable{
private Box b;
public Producer(Box b){
this.b=b;
}
@Override
public void run(){
for(int i=1;i<=5;i++){
b.put();
}
}
}
package learn;
public class Consumer implements Runnable{
private Box b;
public Consumer(Box b){
this.b=b;
}
@Override
public void run(){
while (true){
b.get();
}
}
}
|