本文介绍了使用 Scanner() 读取 CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 csv 正在被读入 System.out,但我注意到任何带有空格的文本都会移动到下一行(作为返回)

这是我的 csv 的启动方式:

first,last,email,address 1, address 2john,smith,[email protected], 123 St. Street,Jane,Smith,[email protected],4455 Roger Cir,apt 2

运行我的应用程序后,任何带有空格(地址 1)的单元格都会被扔到下一行.

import java.io.File;导入 java.io.FileNotFoundException;导入 java.util.Scanner;公共课主要{公共静态无效主(字符串 [] args){//- 在应用程序中定义 .csv 文件String fileNameDefined = "uploadedcsv/employees.csv";//- 将 stringName 转换为实际文件所需的文件类File file = new File(fileNameDefined);尝试{//- 使用 Scanner 类从 filePooped 中读取扫描仪 inputStream = 新扫描仪(文件);//hashNext() 逐行循环while(inputStream.hasNext()){//读取单行,放入字符串字符串数据 = inputStream.next();System.out.println(data + "***");}//循环后,关闭扫描器inputStream.close();}catch (FileNotFoundException e){e.printStackTrace();}}}

所以这是控制台中的结果:

首先,最后,电子邮件,地址1、地址2约翰,史密斯,[email protected],123英石.街道,简,史密斯,[email protected],4455罗杰Cir,apt2

我是否错误地使用了扫描仪?

解决方案
scanner.useDelimiter(",");

这应该可行.

import java.io.File;导入 java.io.FileNotFoundException;导入 java.util.Scanner;公共类 TestScanner {public static void main(String[] args) 抛出 FileNotFoundException {Scanner 扫描仪 = new Scanner(new File("/Users/pankaj/abc.csv"));scan.useDelimiter(",");而(扫描仪.hasNext()){System.out.print(scanner.next()+"|"​​);}扫描仪关闭();}}

对于 CSV 文件:

a,b,c d,e1,2,3 4,5X,Y,Z A,B

输出为:

a|b|c d|e1|2|3 4|5X|Y|Z A|B|

My csv is getting read into the System.out, but I've noticed that any text with a space gets moved into the next line (as a return)

Here's how my csv starts:

first,last,email,address 1, address 2
john,smith,[email protected],123 St. Street,
Jane,Smith,[email protected],4455 Roger Cir,apt 2

After running my app, any cell with a space (address 1), gets thrown onto the next line.

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

public class main {

    public static void main(String[] args) {
        // -define .csv file in app
        String fileNameDefined = "uploadedcsv/employees.csv";
        // -File class needed to turn stringName to actual file
        File file = new File(fileNameDefined);

        try{
            // -read from filePooped with Scanner class
            Scanner inputStream = new Scanner(file);
            // hashNext() loops line-by-line
            while(inputStream.hasNext()){
                //read single line, put in string
                String data = inputStream.next();
                System.out.println(data + "***");

            }
            // after loop, close scanner
            inputStream.close();


        }catch (FileNotFoundException e){

            e.printStackTrace();
        }

    }
}

So here's the result in the console:

first,last,email,address
1,address
2
john,smith,[email protected],123
St.
Street,
Jane,Smith,[email protected],4455
Roger
Cir,apt
2

Am I using Scanner incorrectly?

解决方案
scanner.useDelimiter(",");

This should work.

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


public class TestScanner {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File("/Users/pankaj/abc.csv"));
        scanner.useDelimiter(",");
        while(scanner.hasNext()){
            System.out.print(scanner.next()+"|");
        }
        scanner.close();
    }

}

For CSV File:

a,b,c d,e
1,2,3 4,5
X,Y,Z A,B

Output is:

a|b|c d|e
1|2|3 4|5
X|Y|Z A|B|

这篇关于使用 Scanner() 读取 CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:20