//难道此程序不能运行?? import java.util.jar.*; import java.io.*;public class Test { public static void main(String[] args) throws IOException { JarFile jarFile = new JarFile("d:/temp/test.jar"); JarEntry dbEntry = jarFile.getJarEntry("test.txt"); InputStream in = jarFile.getInputStream(dbEntry);int count = 2; int data = 0; int num = 0; //保存读出来的数字 while ((data = in.read()) != -1) { if (data != '*') { num += Math.pow(10, count--) * (data - 48); } if (count == -1) { System.out.println(num); num = 0; count = 2; } } in.close(); jarFile.close(); } }
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream;/** * @author Administrator * */ public class DataInputStreamTest { /** * @param args */ public static void main(String[] args) { // TODO 自动生成方法存根 try { InputStream is = new FileInputStream("d:/temp/test.txt"); DataInputStream dis = new DataInputStream(is); int count = 2; int data = 0; int num = 0; while ((data = dis.read()) != -1) { if((char)data!='*'){ num += Math.pow(10, count--) * (data - 48); if (count == -1) { System.out.println(num); num = 0; count = 2; } } } dis.close(); is.close(); } catch (FileNotFoundException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } } 文件内容:078060*078058*078060*078058*078060* 输出结果: 78 60 78 58 78 60 78 58 78 60
while ((data = dis.read()) != -1) { if((char)data!='*'){ num += Math.pow(10, count--) * (data - 48); if (count == -1) { System.out.println(num); num = 0; count = 2; } } } //大哥,这段代码什么意思? 可以讲解下么?
while ((data = dis.read()) != -1) {//从dis输入流中读取下一个数据字节。返回一个 0 到 255 范围内的 int 字节值。如果因为已经到达流末尾而没有字节可用,则返回 -1为-1时表示读到了文件末将不执行循环中的代码 //0是48,1是49....这个叫什么来的? if((char)data!='*'){//读到*号时不做处理继续读下一个字节 num += Math.pow(10, count--) * (data - 48);//Math.pow() public static double pow(double a,double b)Returns the value of the first argument raised to the power of the second argument. Special cases:....自己去查 if (count == -1) {//读过3位后输出值,重复此过程. System.out.println(num); num = 0; count = 2; } } }
import java.util.jar.*;
import java.io.*;public class Test {
public static void main(String[] args) throws IOException {
JarFile jarFile = new JarFile("d:/temp/test.jar");
JarEntry dbEntry = jarFile.getJarEntry("test.txt");
InputStream in = jarFile.getInputStream(dbEntry);int count = 2;
int data = 0;
int num = 0; //保存读出来的数字
while ((data = in.read()) != -1) {
if (data != '*') {
num += Math.pow(10, count--) * (data - 48);
}
if (count == -1) {
System.out.println(num);
num = 0;
count = 2;
}
}
in.close();
jarFile.close();
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;/**
* @author Administrator
*
*/
public class DataInputStreamTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根 try {
InputStream is = new FileInputStream("d:/temp/test.txt");
DataInputStream dis = new DataInputStream(is); int count = 2;
int data = 0;
int num = 0;
while ((data = dis.read()) != -1) {
if((char)data!='*'){
num += Math.pow(10, count--) * (data - 48);
if (count == -1) {
System.out.println(num);
num = 0;
count = 2;
}
}
}
dis.close();
is.close();
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
文件内容:078060*078058*078060*078058*078060*
输出结果:
78
60
78
58
78
60
78
58
78
60
if((char)data!='*'){
num += Math.pow(10, count--) * (data - 48);
if (count == -1) {
System.out.println(num);
num = 0;
count = 2;
}
}
}
//大哥,这段代码什么意思? 可以讲解下么?
//0是48,1是49....这个叫什么来的?
if((char)data!='*'){//读到*号时不做处理继续读下一个字节
num += Math.pow(10, count--) * (data - 48);//Math.pow()
public static double pow(double a,double b)Returns the value of the first argument raised to the power of the second argument. Special cases:....自己去查
if (count == -1) {//读过3位后输出值,重复此过程.
System.out.println(num);
num = 0;
count = 2;
}
}
}