Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:从文件中读取由hashtag(#)分隔的句子_Java - Fatal编程技术网

Java:从文件中读取由hashtag(#)分隔的句子

Java:从文件中读取由hashtag(#)分隔的句子,java,Java,我想询问关于在java上从文件打印文本的问题,但它由一个hashtag(#)分隔 以下是文本文件示例: Chicken#2#Theone#1993#NoneRooster#3#Bone, the roost#1992#None 我想要的格式是: Chicken | 2 | Theone | 1993 | None Rooster | 3 | Bone, The Roost | 1992

我想询问关于在java上从文件打印文本的问题,但它由一个hashtag(#)分隔

以下是文本文件示例:

Chicken#2#Theone#1993#NoneRooster#3#Bone, the roost#1992#None
我想要的格式是:

Chicken    |    2    |    Theone             |    1993    |    None
Rooster    |    3    |    Bone, The Roost    |    1992    |    None
我真的不知道ArrayList和文件操作在Java中是如何工作的,如果你们能提供一种详细的方法,那就太好了


提前感谢

假设您的输入是(带行)

那你可以试试这个

String input = "Chicken#2#Theone#1993#None\n" +
            "Rooster#3#Bone, the roost#1992#None";
    try(BufferedReader br = new BufferedReader(new StringReader(input))){
        String line;
        while((line = br.readLine()) != null) {
            String[] cols = line.split("#");
            System.out.print(cols[0]);
            for (int i = 1; i < cols.length; i++) {
                System.out.print("|" + cols[i]);
            }
            System.out.println();
        }
    }
String input=“Chicken#2#Theone#1993#None\n”+
“公鸡3骨,公鸡1992无”;
try(BufferedReader br=newbufferedreader(newstringreader(输入))){
弦线;
而((line=br.readLine())!=null){
字符串[]cols=line.split(“#”);
系统输出打印(cols[0]);
for(int i=1;i
假设您的文件是Text.txt,其中的文本如下所示

Chicken#2#Theone#1993#None
Rooster#3#Bone, the roost#1992#None
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        File file = new File("Test.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));

        String line;
        while ((line = br.readLine()) != null) {
            String[] words = line.split("#");
            for (int i = 0; i < words.length; i++) {
                if (i == words.length - 1) {
                    System.out.print(formatString(words[i], 20));
                } else {
                    System.out.print(formatString(words[i], 20) + "|");
                }
            }
            System.out.println();
        }

    }

    static String formatString(String word, int length) {
        return new String(new char[5]).replace('\0', ' ') + word + new String(new char[length - word.length()]).replace('\0', ' ');
    }
}
你可以很容易地这样做

Chicken#2#Theone#1993#None
Rooster#3#Bone, the roost#1992#None
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        File file = new File("Test.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));

        String line;
        while ((line = br.readLine()) != null) {
            String[] words = line.split("#");
            for (int i = 0; i < words.length; i++) {
                if (i == words.length - 1) {
                    System.out.print(formatString(words[i], 20));
                } else {
                    System.out.print(formatString(words[i], 20) + "|");
                }
            }
            System.out.println();
        }

    }

    static String formatString(String word, int length) {
        return new String(new char[5]).replace('\0', ' ') + word + new String(new char[length - word.length()]).replace('\0', ' ');
    }
}
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileReader;
导入java.io.IOException;
公共班机{
公共静态void main(字符串[]args)引发IOException{
File File=新文件(“Test.txt”);
BufferedReader br=新的BufferedReader(新文件读取器(文件));
弦线;
而((line=br.readLine())!=null){
String[]words=line.split(“#”);
for(int i=0;i
似乎您需要在None和Roosters之间添加下一行,那么您不应该学习如何操作吗?否则,您将在1小时后在这里询问下一个问题的解决方案,依此类推。先生,谢谢您的回答。我会试着把密码通读一遍:)谢谢你的回答,先生。我将试着通读代码:)先生,我想问一下这句话,而((line=br.readLine())!=null)它是用来告诉文件的结尾吗?@KevinTan br.readLine()从您的文本文件中读取这一行。假设文本文件中有5行。然后while循环将迭代5次。直到不再有行为止(没有行时br.readLine()将返回null)。这就是那句话的意思。非常感谢,先生。但是如果我想用我问题中的空格打印出来呢?我仍然不太确定在哪里可以设置格式。@KevinTan我添加了一个设置字符串格式的方法。请检查编辑的代码