读取具有相同分隔符的末尾的文本文件。

huangapple go评论65阅读模式
英文:

Read text file with the same delimiter at the end

问题

public void loadTxt(String path)
{
    Path pathToFile = Paths.get(path.replace("\\", "\\\\"));
    try (BufferedReader br = Files.newBufferedReader(pathToFile,
            StandardCharsets.US_ASCII)) {
        
        String line = br.readLine();
        while (line != null) {
            String[] attributes = line.split(";");
            line = br.readLine();
        }
        
    }
    
    catch (IOException ioe) 
    {
        ioe.printStackTrace();
    }
}

文本数据如下:

1;5;emitter;
2;7;jim;
3;2;ert;
英文:

I have 2 problems with my loadTxt method.

1- I get the path as a file[].toString type from another method and the path look like this:

C:\files\texts\text1.txt

But I need the path with 2 backsplashes for nio.file.Path like this:

C:\\files\\texts\\text1.txt

2 - The second problem is with the loadtxt method. I think this is because of the same delimiter ";" at the end the text file I get a java.nio.charset.MalformedInputException: Input length = 1 .

public void loadTxt(String path)
{
	Path pathToFile = Paths.get("z:\\Filiallager_21_Regal_1.txt");
	try (BufferedReader br = Files.newBufferedReader(pathToFile,
            StandardCharsets.US_ASCII)) {
		
        String line = br.readLine();
        while (line != null) {
            String[] attributes = line.split(";");            
            line = br.readLine();    
        }
        
	}
	
	catch (IOException ioe) 
	{
        ioe.printStackTrace();
    }
}

My texts data look like this:

1;5;emitter;
2;7;jim;
3;2;ert;

答案1

得分: 0

使用分隔符进行拆分时,最好测试数组的长度,以确保不超出其边界:

String line = br.readLine();
while (line != null) {
    String[] attributes = line.split(";");
    if (attributes.length > 3) {
        System.out.println(attributes[3]);
    }
    line = br.readLine();
}

或者,您可以使用循环:

for (int x = 0; x < attributes.length; x++) {
    System.out.println(attributes[x]);
}

编辑

根据您的文件读取错误,我认为您最好使用 FileReader

BufferedReader br = new BufferedReader(new FileReader("foo.txt"));
英文:

When splitting using a delimiter it is best to test against the length of the array to ensure that you do not exceed its bounds:

String line = br.readLine();
while (line != null) {
    String[] attributes = line.split(&quot;;&quot;);   
    if (attributes.length &gt; 3) {
        System.out.println (attributes[3]);
    }     
    line = br.readLine();    
}

or you could use a loop

for (int x = 0; x &lt; attributes.length; x++)
{
    System.out.println (attributes[x]);
}

edit

As per the error reading your file, I think you are better off using a FileReader

BufferedReader br = new BufferedReader(new FileReader(&quot;foo.txt&quot;));

答案2

得分: -1

问题1:
你可以使用path = path.replace("\\", "\\\\");将斜杠替换为双斜杠。

问题2:
我真的没有理解问题出在哪里,我使用了你的代码和示例数据,对我来说运行正常。请提供异常详细信息,或确认所提供的数据是否正确。

英文:

For Problem 1:
You can replace slash with double slash using path = path.replace(&quot;\\&quot;, &quot;\\\\&quot;);

For Problem 2:

I really did not get what is the issue, I tried with your code and sample data and it is working fine for me. Either provide exception details or if the data you provided is correct or not.

huangapple
  • 本文由 发表于 2020年10月2日 14:08:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64166865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定