日期:2014-05-20  浏览次数:20707 次

JAVA 基础问题,在线等,有答案立刻结贴。
一个txt文件放在jar中,txt文件的内容:078060*078058*078060*078058*078060*,请写出程序读取出78   、60、78、58……这样的数字.(要求给出能运行的程序,谢谢大家)

------解决方案--------------------
//难道此程序不能运行??
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) {//从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;
}
}
}