从Java中读取txt文件

时间:2015-02-12 20:18:50

标签: java c file-io io

我现在正在上C课程,但我也想用Java练习我的工作。我在从Java文件中读取时遇到问题(我希望从文件中获取不同的类型(double,int等)并将其存储在一些变量中。我知道在C中,代码就像这样:

int main(void) {

    FILE* fp;
    char name[29];
    int qty;
    char item[20];
    float price;

    fp= fopen("input.txt","r");
    if (fp == NULL) {
        printf("Couldn't open the file.");
        return;
    }

    while (fscanf(fp, "%s %d %s %.2f\n", name, &qty, item, &price) != NULL) {
        // do something
    }

    return 0;
}

但是在Java中呢?到目前为止我已经做到了这一点,但它不起作用。我确实得到了输出,但格式不是我想要的。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main_Class {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner scan = null;
    try {
        scan = new Scanner(new File("input.txt"));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    while (scan.hasNextLine()) {
        String name = "";
        int quantity = 0;
        String item_name = "";
        double price = 0.0;

        if(scan.hasNext()) {
            name = scan.next();
        } 
        if(scan.hasNextInt()) {
            quantity = scan.nextInt();
        } 
        if(scan.hasNext()) {
            item_name = scan.next();
        } 
        if(scan.hasNextDouble()) {
            price = scan.nextDouble();
        }
        System.out.println(name + " " + quantity + " " + item_name + " " +    price);
    }
}

}
编辑:对不起,伙计们,我忘了包含txt文件内容。我希望我的输出就像这样。

Smith 3 Sweater $22.50
Reich 3 Umbrella $12.50
Smith 1 Microwave $230.00
Lazlo 1 Mirror $60.00
Flintstone 5 Plate $10.00
Lazlo 1 Fridge $1200.00
Stevenson 2 Chair $350.00
Smith 10 Candle $3.50
Stevenson 1 Table $500.00
Flintstone 5 Bowl $7.00
Stevenson 2 Clock $30.00
Lazlo 3 Vase $40.00
Stevenson 1 Couch $800.00

我的输出(在Java中,我只是要包括其中一些,因为它们很长):

Smith 3 Sweater 0.0
$22.50 0 Reich 3.0
Umbrella 0 $12.50 0.0
Smith 1 Microwave 0.0
$230.00 0 Lazlo 1.0
Mirror 0 $60.00 0.0
Flintstone 5 Plate 0.0
$10.00 0 Lazlo 1.0

2 个答案:

答案 0 :(得分:2)

if(scan.hasNextDouble())
{
    price = scan.nextDouble();
}

将此更改为

 price = Double.parseDouble(scan.next().substring(1));

并改变

 System.out.println(name + " " + quantity + " " + item_name + " " +    price);

这个

 System.out.println(name + " " + quantity + " " + item_name + " $" +    price);

答案 1 :(得分:1)

如果您只关心打印数据,那么您也可以将所有内容作为字符串读取并简化代码:

while (scan.hasNextLine()) {
    String name, quantity, item_name, price;

    if(scan.hasNext()) {
        name = scan.next();
    }
    else ...

    if(scan.hasNext()) {
        quantity = scan.nextInt();
    } 
    else ...

    if(scan.hasNext()) {
        item_name = scan.next();
    }
    else ...

    if(scan.hasNext()) {
        price = scan.nextDouble();
    }
    else ...

    System.out.println(name + "\t" + quantity + "\t" + item_name + "t" +    price);
}

制表符'\ t'使输出看起来更好一点。在其他地方,你可以设置默认值,以防万一没有找到。

如果您确实关心某事是int还是double,您可以使用解析方法,即

int int_quantity = Integer.parseInt( quantity );