标签:byw999
一:*/
public class StringDemo {
public static void main(String[] args) {
//定义一个字符串
// String str = "abc" ;
// String str = "我爱你中国" ;
String str = "你好" ;//[-60, -29, -70, -61]
//转成字节数组
byte[] bys = str.getBytes() ;
// System.out.println(bys);
//需要能看懂
//Arrays
//System.out.println(Arrays.toString(bys));//[97, 98, 99] :英文的字符串
System.out.println(Arrays.toString(bys));
//[-50, -46, -80, -82, -60, -29, -42, -48, -71, -6]
}
}
2.
一次读取一个字节数组的方式要比一次读取一个字节方式高效.
*
*/
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
//复制操作:将e盘下的abc.mp4复制到当前项目下,输出copy.mp4
//封装源文件
FileInputStream fis = new FileInputStream("e://abc.mp4") ;
//封装目的地文件
FileOutputStream fos = new FileOutputStream("copy.mp4") ;
//读写操作:一次读取字节数组
byte[] bys = new byte[1024] ;
int len = 0 ;
while((len=fis.read(bys))!=-1) {
//边读边写
fos.write(bys, 0, len);
}
//释放资源
fis.close();
fos.close();
}
}
3.字节缓冲输入流
*在使输入流的时候,
*/
public class BufferedInputStreamDemo {
public static void main(String[] args) throws Exception {
//构造一个字节缓冲输入流对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bos.txt"));
//读数据
//一次读取一个字节
/ int by = 0 ;
while((by=bis.read())!=-1) {
System.out.print((char)by);
}/
//一次读取一个字节数组
byte[] bys = new byte[1024] ;
int len = 0 ;
while((len=bis.read(bys))!=-1) {
System.out.println(new String(bys, 0, len));
}
//释放资源
bis.close();
}
}
4.字节缓冲输出流:
5.存储文件
StringBuffer:提供了一个字符串缓冲区 (可以在缓冲区中不断追加字符串)
/
6.
使用字节流一次读取一个字节的方式,会造成中文乱码--->Java提供了一个字符流(专门用来解决中文乱码问题)
*/
public class FileInputStreamDemo {
public static void main(String[] args) throws Exception {
//封装文件
//一次读取一个字节的方式
FileInputStream fis = new FileInputStream("a.txt") ;
//读数据
int by = 0 ;
while((by=fis.read())!=-1) {
System.out.print((char)by);
}
//释放资源
fis.close();
}
}
7.编码和解码:前后的编码格式要一致!
*/
public class StringDemo {
public static void main(String[] args) throws Exception {
//定义一个字符串
String str ="你好" ;
//编码和解码:前后必须一致
//编码
// byte[] bys = str.getBytes() ;
byte[] bys = str.getBytes("utf-8") ;//-28, -67, -96, -27, -91, -67]
System.out.println(Arrays.toString(bys));//[-60, -29, -70, -61]
System.out.println("------------------");
//解码
// public String(byte[] bytes) :使用平台默认编码集(gbk)
// String s = new String(bys) ;
// String s = new String(bys,"gbk") ;//一个中文对应三个字节
String s = new String(bys,"utf-8") ;//一个中文对应三个字节
System.out.println(s);
}
}
8.需求:将a.txt文件中的内容进行复制,复制到当前项目下(b.txt)
11.字符输入流读数据的方法:
*flush和close方法的区别?
*字符缓冲输入流/字符缓冲输出流
*杂七杂八的流(properties:属性集合类/合并流/序列化Serializable/内存操作流)
*先使用字符缓冲输出流写数据,在使用字符缓冲输入读数据,显示控制台上
*字符缓冲输出流:
*字符缓冲输入流:
*分别使用两种方式
*/
public class CopyDemo {
public static void main(String[] args) throws Exception {
//源文件:StringDemo.java
//目的地文件:当前项目下copy.java
//封装文件
BufferedReader br = new BufferedReader(new FileReader("StringDemo.java")) ;
//封装目的地
BufferedWriter bw = new BufferedWriter(new FileWriter("copy.java")) ;
//一次读取一个字符数组
/ char[] chs = new char[1024] ;
int len = 0 ;
while((len=br.read(chs))!=-1) {
bw.write(chs, 0, len);
bw.flush();
}/
//一次读取一行
String line = null ;
while((line=br.readLine())!=null) {
//写
bw.write(line);
bw.newLine();
bw.flush();
}
//关闭资源
bw.close();
br.close();
}
}
*/
public class ArrayListToFileTest {
public static void main(String[] args) throws Exception {
//创建一个ArrayList集合
ArrayList<String> list = new ArrayList<String>() ;
//添加元素
list.add("hello") ;
list.add("world") ;
list.add("java") ;
list.add("hello") ;
//创建一个字符缓冲输出流
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt")) ;
//遍历
for(String s:list) {
//将集合中元素写入到流中
bw.write(s);
bw.newLine();
bw.flush();
}
//关闭资源
bw.close();
}
}
18.复制文本文件(5种方式分别完成)
*/
public class CopyTest {
public static void main(String[] args) throws Exception {
// method1("StringDemo.java","copy.java") ;
method2();
}
private static void method2() throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader("StringDemo.java")) ;
BufferedWriter bw = new BufferedWriter(new FileWriter("copy.java")) ;
//一次读取一行
String line = null;
while((line=br.readLine())!=null) {
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
private static void method1(String src, String dest) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(src)) ;
BufferedWriter bw = new BufferedWriter(new FileWriter(dest)) ;
//字符数组
char[] chs = new char[1024] ;
int len = 0 ;
while((len=br.read(chs))!=-1) {
bw.write(chs, 0, len);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
19.需求:有一个文本文本,需要将文本文件中的内容放到ArrayList集合中,遍历集合获取元素
源文件:b.txt----->读取---->BuffferedReader
目的地:ArrayList<String>
*/
public class FileToArrayListTest {
public static void main(String[] args) throws Exception {
//封装源文件
BufferedReader br = new BufferedReader(new FileReader("b.txt")) ;
//创建一个ArrayList集合
ArrayList<String> list = new ArrayList<String>() ;
//读b.txt文件的内容
String line = null ;
while((line=br.readLine())!=null) {
//将数据添加到集合中
list.add(line) ;
}
//遍历集合
for(String s:list) {
System.out.println(s);
}
//关闭流
br.close();
}
}
20.内存操作流:适用于临时存储文件.
21.打印流
*PrintWriter:属于输出流
4)打印的方法:print(XXX x)/println(XXX xx)
二.
序列化:将对象按照流的方式存储到文本文件中或者再网络中传输 对象---->流数据 序列化流 (ObjectOutputStream)
*反序列化:将文本文件中的流对象或者网络传输中的流对象还原成对象 流数据--->对象 反序列化流(ObjectInputStream)
现在就自定义类:Person类
/
public class ObjectDemo {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
// write() ;
read();
}
//反序列化
private static void read() throws FileNotFoundException, IOException, ClassNotFoundException {
//创建反序列化流对象
//public ObjectInputStream(InputStream in)
ObjectInputStream in = new ObjectInputStream(new FileInputStream("oos.txt")) ;
//读
//public final Object readObject():从 ObjectInputStream 读取对象。
Object obj = in.readObject() ;
in.close();
System.out.println(obj);//Person [name=高圆圆, age=27]
}
//序列化
private static void write() throws FileNotFoundException, IOException {
//创建一个序列化流对象
//public ObjectOutputStream(OutputStream out)
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oos.txt")) ;
//创建一个Person类对象
Person p = new Person("高圆圆", 27) ;
//public final void writeObject(Object obj)
oos.writeObject(p);
//关闭资源
oos.close();
}
}
2.java.io.NotSerializableException :当前类未实现序列化功能的异常
*/
public class PropertiesDemo {
public static void main(String[] args) {
//它继承Hashtable
//创建一个属性集合类对象
// Properties<String,String> prop = new Properties<String,String>() ;
Properties prop = new Properties() ;
System.out.println(prop);
System.out.println("---------------------");
//给属性集合类中的属性列表添加元素
prop.put("高圆圆", "赵又廷") ;
prop.put("文章", "马伊琍") ;
prop.put("黄晓明", "baby") ;
System.out.println(prop);
//遍历属性集合类
Set<Object> keySet = prop.keySet() ;
for(Object key :keySet) {
Object value = prop.get(key) ;
System.out.println(key+"="+value);
}
}
}
4.Properties:表示了一个持久的属性集(简称:属性集合类) extends Hashtable<K,V> Map集合的
*/
public class PropertiesDemo {
public static void main(String[] args) {
//它继承Hashtable
//创建一个属性集合类对象
// Properties<String,String> prop = new Properties<String,String>() ;
Properties prop = new Properties() ;
System.out.println(prop);
System.out.println("---------------------");
//给属性集合类中的属性列表添加元素
prop.put("高圆圆", "赵又廷") ;
prop.put("文章", "马伊琍") ;
prop.put("黄晓明", "baby") ;
System.out.println(prop);
//遍历属性集合类
Set<Object> keySet = prop.keySet() ;
for(Object key :keySet) {
Object value = prop.get(key) ;
System.out.println(key+"="+value);
}
}
}
5.属性集合类的特有功能:
*/
public class PropertiesDemo2 {
public static void main(String[] args) {
//创建属性集合类对象
Properties prop = new Properties() ;
//添加元素
prop.setProperty("张三", "20") ;
prop.setProperty("李四", "22") ;
prop.setProperty("王五", "18") ;
//遍历
//获取所有的键的集合
Set<String> keyset = prop.stringPropertyNames() ;
for(String key:keyset) {
//通过键找值
String value = prop.getProperty(key) ;
System.out.println(key+"----"+value);
}
}
}
6.可保存在流中或从流中加载,只能使用属性集合类
public void store(Writer writer,String comments):把集合中的数据保存文本文件中(属性集合)
public void load(Reader reader):将文本文件中的数据加载到属性集合中
7.可保存在流中或从流中加载,只能使用属性集合类
public void store(Writer writer,String comments):把集合中的数据保存文本文件中(属性集合)
public void load(Reader reader):将文本文件中的数据加载到属性集合中
*举例:
打游戏:游戏进度的保存和游戏加载
*/
public class PropertiesDemo3 {
public static void main(String[] args) throws IOException {
// MyStore();
MyLoad();
}
//将文本文件中的数据加载属性集合类中
private static void MyLoad() throws IOException {
//创建属性集合类对象
Properties prop =new Properties() ;
//public void load(Reader reader):将文本文件中的数据加载到属性集合中
FileReader fr = new FileReader("prop.txt") ;
//加载
prop.load(fr);
fr.close();
System.out.println(prop);
}
//将属性集合中的数据保存到文本文件中
private static void MyStore() throws IOException {
//创建一个属性集合类对象
Properties prop = new Properties() ;
//添加元素
prop.setProperty("张三", "20") ;
prop.setProperty("文章", "29") ;
prop.setProperty("成龙", "55") ;
//public void store(Writer writer,String comments):把集合中的数据保存文本文件中(属性集合)
FileWriter fw = new FileWriter("name.txt") ;
//将数据保存到文本文件中
prop.store(fw, "names‘content");
//释放资源
fw.close();
}
}
我有一个文本文件(user.txt),我知道数据是键值对形式的,但是不知道内容是什么。
请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其实为”100
1)读取文件的内容,将文件内容加载属性集合类中
2)遍历属性集合,获取所有的键的集合
3)遍历的键的时候,可以判断是否有"lisi"这样一个键
4)有的话,就更改
5)需要将当前属性集合类中的保存文本文件中
/
public class PropertiesTest {
public static void main(String[] args) throws IOException {
//创建属性集合类对象
Properties prop = new Properties() ;
//读取文本文件内容加载到集合中
FileReader fr = new FileReader("user.txt") ;
prop.load(fr);
fr.close();
//遍历属性集合
//获取所有的键的集合
Set<String> keySet = prop.stringPropertyNames() ;
for(String key:keySet) {
//判断
if("lisi".equals(key)) {
//更改
prop.setProperty(key, "100") ;
}
}
//将属性集合中的数据保存文本文件中
FileWriter fw = new FileWriter("user.txt") ;
prop.store(fw, "content");
fw.close();
}
}
9.JVM:Java虚拟机 识别main(主线程)
并行和并发(高并发:MyBatis --->IBatis:半自动化)
强者逻辑上的同时,指的是同一个时间段内
*后者物理上的同时,指的是同一个时间点
*/
public class ThreadDemo {
public static void main(String[] args) {
//创建MyThread类对象
// MyThread my = new MyThread() ;
//当前Thread类有一个run public void run()
// my.run();
// System.out.println("-------------------");
// my.run();
//执行线程不是run方法 ,run方法的调用相当于一个普通方法的调用
/*public void start()使该线程开始执行;Java 虚拟机调用该线程的 run 方法。
结果是两个线程并发地运行*/
MyThread t1 = new MyThread() ;
t1.start();
//IllegalThreadStateException:非法状态异常,同一个线程只能被执行一次
// t1.start();
MyThread t2 = new MyThread() ;
t2.start();
}
}
11.Thread 类提供了一些方法
12.*public final void setDaemon(boolean on) :true时,表示为守护线程
13.跟线程优先级相关的方法:
public static final int MAX_PRIORITY 10 最大优先级
public static final int MIN_PRIORITY 1 最小优先级
public static final int NORM_PRIORITY 5 默认优先级
*/
public class ThreadDemo {
public static void main(String[] args) {
//创建三个子线程
MyThread t1 = new MyThread() ;
MyThread t2 = new MyThread() ;
MyThread t3 = new MyThread() ;
// System.out.println(t1.getPriority()); //5 默认优先级
// System.out.println(t2.getPriority());
// System.out.println(t3.getPriority());
t1.setName("林青霞");
t2.setName("林志颖");
t3.setName("×××");
//设置线程优先级
t1.setPriority(10);
t2.setPriority(1);
t3.setPriority(5);
t1.start();
t2.start();
t3.start();
}
}
14.public static void sleep(long millis):线程睡眠 指定是时间毫秒值
*/
public class ThreadDemo {
public static void main(String[] args) {
//创建当前类对象
MyThread my =new MyThread() ;
//实现多线程
//public Thread(Runnable target,String name)
Thread t1 = new Thread(my, "高圆圆") ;
Thread t2 = new Thread(my, "赵又廷") ;
//启动线程
t1.start();
t2.start();
}
}
标签:byw999
原文地址:http://blog.51cto.com/13677893/2119579