您的位置:首页 > 编程语言 > Java开发

Java 重要对象

2013-04-01 09:57 274 查看
System对象:

import java.util.Properties;
//import java.io.*;
import static System.out.Sop.*;

public class SystemDemo {
public static void main(String[] args) {
Properties prop=System.getProperties();
//在系统中自定义一些特有的信息
System.setProperty("mykey","myvalue");
//Stringvalue=System.getProperty("os.name");
//sop("value="+value);
//在jvm启动时,动态的加载一些属性信息
//java -Dhaha-qqqqqSystemDemo
//获取所有属性信息
for(Object obj:prop.keySet()){
String value=(String)prop.get(obj);
sop(obj+":::"+value);
//sop("hello");
}
}
}


Runtime对象:

import java.io.IOException;

public class RuntimeDemo {
public static void main(String[] args) {
Runtime r = Runtime.getRuntime();
try {
r.exec("C:\\Program Files(x86)\\Tencent\\QQ\\Bin\\QQ.exe");
r.exec("notepad.exe");
} catch (IOException e) {
e.printStackTrace();
}
}
}


Calendar对象:

import java.util.Calendar;
import java.util.GregorianCalendar;
import static System.out.Sop.*;

public class CalendarDemo {
public static void main(String[] args) {
Calendar c=new GregorianCalendar();//Calendar c1=Calendar.getInstance();
printCalendar(c);
c.add(Calendar.MONTH,-3);
printCalendar(c);
getDays(2015);
}
public static void printCalendar(Calendar c){
sop(c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"+c.get(Calendar.DAY_OF_MONTH)+"日");
}
public static void getDays(int year){//获取任意年的2月有多少天
Calendar c=new GregorianCalendar();
c.set(year,2,1);//某年的3月1日
c.add(Calendar.DAY_OF_MONTH,-1);
int days=c.get(Calendar.DAY_OF_MONTH);
sop(days);
}
}


Math-Random对象:

import static System.out.Sop.sop;
import java.util.Random;

public class MathDemo {
public static void main(String[] args) {
Random r = new Random();
for (int i = 0; i < 10; i++) {
// int d=(int)(Math.random()*10+1);
sop(r.nextInt(10));
}
show();

}

public static void show() {
double d = Math.ceil(-16.34);// 返回大于参数的最小整数
double d1 = Math.floor(12.34);// 返回小于参数的最大整数
long l = Math.round(12.34);// 四舍五入
long l1 = Math.round(12.54);
sop("2的三次幂:" + Math.pow(2, 3));
sop("d:" + d);
sop("d1:" + d1);
sop("l:" + l);
sop("l1:" + l1);
}
}


IO:
gb2312--扩容-àgbk
Unicode(国际标准码表)—转换优化àUTF-8;
字符流(揉和各种编码表)基于字节流

字节流的抽象基类:
InputStream,OutputStream
字符流的抽象基类:
Reader,Writer
注:由着四个类派生出来的子类名称都是以其父类名作为子类名的后缀。如:InputStream的子类FileInputStream;reader的子类FileReader。
后缀名是父类名,前缀名该对象的功能

import java.io.FileWriter;
import java.io.IOException;

public class FileWriterDemo {
public static void main(String[] args) throws IOException {
// 创建一个FileWriter对象,该对象一倍初始化就必须要明确被操作的文件
// 而且该文件会创建到指定目录下,如果该目录下已有同名文件,将覆盖之
// 明确数据要存放的位置
FileWriter fw = new FileWriter("demo.txt");
// 调用write方法,将字符串写入到流中
fw.write("我是帅哥");
// 将数据刷到目的地中
fw.flush();
fw.write("哈哈");
// 关闭流资源,但是关闭之前会刷新一次内部缓冲中的数据
// 和flush的区别:flush刷新后,流可以继续使用,close刷新后,会将流关闭
fw.close();
}
}


IO异常的处理方式:

import java.io.FileWriter;
import java.io.IOException;
import static System.out.Sop.*;

public class FileWriterDemo2 {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("demo1.txt");
fw.write("我是大帅哥");
} catch (IOException e) {
sop(e.toString());
} finally {
try {
if (fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


文件的续写:

import java.io.FileWriter;
import java.io.IOException;

public class FileWriterDemo3 {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("demo.txt", true);
fw.write("我叫阿信\r\n我是大帅哥");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null)
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


文本文件读取方式:
方式一:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import static System.out.Sop.*;

public class FileReaderDemo {
public static void main(String[] args) {
// 创建一个文件读取流对象,和指定名称的文件相关联
// 要保证该文件是已经存在的,如果不存在,会发生FileNotFoundException异常
FileReader fr = null;
StringBuilder sb = new StringBuilder();
try {
fr = new FileReader("demo.txt");
// 调用读取流对象的read方法
// read()方法一次只读一个字符,并会自动往下读
int ch = 0;
while ((ch = fr.read()) != -1) {
sb.append((char) ch);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null)
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
sop(sb.toString());
}
}


方式二:比方式一效率高

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import static System.out.Sop.*;

public class FileReaderDemo2 {
public static void main(String[] args) {
// 定义一个字符数组,用于存储读到的字符
// 该read(char[]ch)方法返回的是读到的字符的个数
char[] ch = new char[1024];
int num = 0;
FileReader fr = null;
try {
fr = new FileReader("demo.txt");
while ((num = fr.read(ch)) != -1) {
sop(new String(ch, 0, num));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null)
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


拷贝文本文件:
复制原理:将一个文件中的数据存储到另一个文件
步骤:
1. 创建一个文件,用于存储要被复制文件中的数据
2. 定义读取流和文件关联
3. 通过不断的读写完成数据存储。
4. 关闭资源

import java.io.*;

public class CopyFileDemo {
public static void main(String[] args) {
// copy_1();
copy_2();// 性能优
}

// 读一个字符就写一个字符
public static void copy_1() {
// 创建目的地
FileWriter fw = null;
// 与已有文件关联
FileReader fr = null;
try {
fw = new FileWriter("C:\\Users\\lenovo\\Desktop\\copytest.txt");
fr = new FileReader("C:\\Users\\lenovo\\Desktop\\BlackHorse.txt");
int ch = 0;
while ((ch = fr.read()) != -1) {
fw.write(ch);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {// 释放资源
if (fr != null)
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
if (fw != null)
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

public static void copy_2() {
// 创建目的地
FileWriter fw = null;
// 与已有文件关联
FileReader fr = null;
// 定义一个字符数组,用于存储读到的字符
// 该read(char[]ch)方法返回的是读到的字符的个数
char[] ch = new char[1024];
int num = 0;
try {
fw = new FileWriter("C:\\Users\\lenovo\\Desktop\\copytest.txt");
fr = new FileReader("C:\\Users\\lenovo\\Desktop\\BlackHorse.txt");
while ((num = fr.read(ch)) != -1) {
fw.write(ch, 0, num);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {// 释放资源
if (fr != null)
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
if (fw != null)
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: