Java I/O常用流示例
package io;
import java.io.*;
/**
* @author 高枕吴忧
* 利用缓冲区原理,BufferedInputStream,
* 实现的文件字节流读取功能示范
*
*/
public class BufferedInOutputStream {
public BufferedInOutputStream() {
ioTest2();
}
public void ioTest2() {
FileInputStream in = null ;
BufferedInputStream bi = null;
int i= 0;
int count=0;
try{
in = new FileInputStream("D:/j/o/java2.txt");
bi = new BufferedInputStream(in);
while(( i=bi.read())!=-1) {
System.out.print((char)i);
count++;
}
System.out.println(count);
in.close();
bi.close();
}catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
new BufferedInOutputStream();
}
}
----------------------------------
package io;
import java.io.*;
/**
* @author Owner 利用缓冲区原理,BufferedInputStream,
* 实现的文件字符流读取和写入功能示范
*/
public class BufferedReaderWriter {
public BufferedReaderWriter() {
bufferRdWter();
}
public void bufferRdWter() {
FileReader fr = null;
FileWriter wt = null;
BufferedReader br = null;
BufferedWriter bw = null;
String s = "";
try {
fr = new FileReader("D:/j/o/java.txt");
br = new BufferedReader(fr);
wt = new FileWriter("D:/j/o/javat2.txt");
bw = new BufferedWriter(wt);
while ((s = br.readLine()) != null) {
bw.write(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.flush();
wt.close();
bw.close();
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new BufferedReaderWriter();
}
}
--------------------------------
package io;
import java.io.*;
/**
* @author 高枕吴忧
* 数据流,注意的是先进先出.数据流方便我们用IO输出基本数据类型的值.比如它提供的readDouble();
*
*/
public class DataInOutputStr
相关文档:
java.lang.NullPointerException
这个异常大家肯定都经常遇到,异常的解释是 "程序遇上了空指针 ",简单地说就是调用了未经初始化的对象或者是不存在的对象,这个错误经常出现在创建图片,调用数组这些操作中,比如图片未经初始化,或者图片创建时的路径错误等等。对数组操作中出现空指针,很多情况下是一些刚开始学习编程 ......
JAVA定时器(java.util.Timer)
2009年07月30日 星期四 下午 02:17
【1】Java 定时器(java.util.Timer)有定时触发计划任务的功能,通过配置定时器的间隔时间,在某一间隔时间段之后会自动有规律的调用预先所安排的计划任务(java.util.TimerTask)。与每个 Timer 对象相对应的是单个后台线程,用于顺序地执行所有计时器 ......
1如何将字串 String 转换成整数 int?
A. 有两个方法:
1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
注: 字串转成 Double, Float, Long 的方法大同小异.
2 如何将整数 int 转换成字串 String ?
A. 有叁种方法:
1.) ......
Java新手留意:Java编程三十条规则
(1) 类名首字母应该大写。字段、方法以及对象(句柄)的首字母应小写。对于所有标识符,其中包含的所有单词都应紧靠在一起,而且大写中间单词的首字母。例如:
ThisIsAClassName
thisIsMethodOrFieldName
若在定义中出现了常数初始化字符,则大写stati ......
今天看到一道题目,是这样的:(我在里面打印了一些语句,先注释掉了)
class Singleton {
private static Singleton obj= new Singleton();
public static int counter1;
public static int counter2 = 2;
private Singleton() {
counter1++;
counter2++;
// System.out.println("Singleton counter1:" ......