文件是酱紫的:num.txt
2.3 1
3.5 2
2.2 0
.
.
.现在想将他们读出来,(一行中分别存到 double 跟int 型的变量里面)。 只是读出来这么简单。请教大哥了。 小弟刚从C++转营过来。//java 的IO还真TM麻烦,如果有fscanf 这样的就好了。
2.3 1
3.5 2
2.2 0
.
.
.现在想将他们读出来,(一行中分别存到 double 跟int 型的变量里面)。 只是读出来这么简单。请教大哥了。 小弟刚从C++转营过来。//java 的IO还真TM麻烦,如果有fscanf 这样的就好了。
解决方案 »
- 在做一个类似intellij里的代码编辑器,未完成还在完善
- 求个swing的布局。。。。。。。。
- 控制台输出“使用或覆盖了已过时的 API。”是怎么回事啊。跪求解答
- 很简单的问题,很容易拿分.
- java专盗qq的木马程序如何实现,给个思路...
- 求助 两个 数组 差集 ,或交集
- java client端的socket默认是阻塞的吗?如果我不断的发送数据给server,也就是只write(),发现连者发送的数据有的发送了,有的没有发送,请问
- setPreferredSize()方法的作用是什么?
- Java如何进行底层开发?
- 字符串的运行结果的具体分析
- swing jinternalframe????
- 神奇的java.util.EventObject
String line = null;
while((line=reader.readLine())!=null){
double a = Double.parseDouble(line);
}只提供个思路,代码没测试
import java.io.File;
import java.io.FileReader;
import java.util.Arrays;public class Main {
public static void main(String[] args) throws Exception {
double[] ds = new double[3];
int[] ins = new int[3];
int i = 0;
BufferedReader br = new BufferedReader(new FileReader(new File(
"d:/num.txt")));
String s = "";
while ((s = br.readLine()) != null) {
String[] ss = s.split(" ");
ds[i] = Double.parseDouble(ss[0]);
ins[i++] = Integer.parseInt(ss[1]);
}
System.out.println(Arrays.toString(ds)+","+Arrays.toString(ins));
}
}
浮点数用: (\\-?\\d*)\\.?\\d+\\-:代表负号 ?:负号出现0次或1次 \\d:代表数字0-9 *:代表数字出现0到多次 \\.:代表小数点 +:代表数字出现一次或多次
JAVA读取是很方便的啊。用Scanner就行了。代码如下:import java.util.*;
import java.io.*;public class ReadTxt {
public static void main(String[] args) throws Exception {
// TODO 自动生成方法存根 Scanner sc=new Scanner(new File("c:/data.txt"));
while(sc.hasNext())//还有数据吗?
{
System.out.println(sc.nextDouble()+" "+sc.nextInt());//读每行的第一个实数,第二个整数
}
sc.close();
}}运行结果:
2.3 1
3.5 2
2.2 0
public class ReadFile
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new FileReader("num.txt "));
String s=null;
while(br.ready())
{
s=br.readLine();
System.out.println("所读入的字符串是: "+s);
}
}
}然后在"num.txt"中输入,
2.3 1
3.5 2
2.2 0
放在同一目录。
import java.io.*;/**
*
* @author scj
*/
public class Main { /**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
RandomAccessFile ra = new RandomAccessFile("a.txt", "rw");
String s = "";
while ((s = ra.readLine()) != null) {
System.out.println(s);
}
}
}
ArrayList arrDouble = new ArrayList();
ArrayList arrInt = new ArrayList();
BufferedReader readFile = null;
try
{
readFile = new BufferedReader(new FileReader(new File(
"fileName")));
String line = "";
while ((line = readFile.readLine()) != null)
{
String[] arrList = line.split(" ");
for (int i = 0; i < arrList.length; i++)
{
if (arrList[i].trim().length() == 0)
{
continue;
}
else if (Pattern.matches("^[0-9]{1,10}$", arrList[i]))
{
arrInt.add(Integer.valueOf(arrList[i]));
}
else if (Pattern.matches("^[0-9]{1,10}.[0-9]{1,10}$",
arrList[i]))
{
arrDouble.add(Double.valueOf(arrList[i]));
}
else
{
System.out.println("Error");
}
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
if (readFile != null)
{
readFile.close();
}
}