繁体   English   中英

从Java中的txt文件读取

[英]Reading from a txt file in Java

我现在正在上一门C课程,但是我也想用Java练习我的工作。 我无法从Java文件中读取文件(我想从文件中获取不同类型(双精度,整数等)并将其存储在某些变量中。我知道在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
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);

如果您只关心打印数据,则不妨将所有内容作为字符串读取,并将代码简化为:

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,则可以使用parse方法,即

int int_quantity = Integer.parseInt( quantity );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM