gpt4 book ai didi

java - 反转整个文本段落而不是反转每个连续行

转载 作者:行者123 更新时间:2023-12-01 14:05:24 25 4
gpt4 key购买 nike

我试图使用 FileInputStream 读取文本文件,并希望反向显示所有单词(因此最后一个单词将是第一个单词等)我能够使用字符串标记器反向显示所有字母,但不是文字。我现在尝试了几种不同的方法,但似乎无法做到这一点

使用 ArrayList 我可以反转行(先打印最后一行,最后打印第一行)..但我找不到反转所有单词的方法

这是我的“test.txt”文件中的内容:

One characteristic of Java is portability, which means that computer programs written in the Java language must run similarly on any hardware/operating-system platform. This is achieved by compiling the Java language code to an intermediate representation called Java bytecode, instead of directly to platform-specific machine code. Java bytecode instructions are analogous to machine code, but they are intended to be interpreted by a virtual machine (VM) written specifically for the host hardware. End-users commonly use a Java Runtime Environment (JRE) installed on their own machine for standalone Java applications, or in a Web browser for Java applets.

代码:

import java.io.*;  
import java.util.ArrayList;


public class RevWords6
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt"), "UTF-8"));

String lines = br.readLine();
ArrayList<String> buffer = new ArrayList<String>();

//while the data is not null
while(lines != null)
{
//String words[] = lines.split(" ");

//track formatting
if(lines != null)
{
buffer.add("\n");
buffer.add(lines); //I wanted to put the words array in here instead of "lines" but didn't know how
}

lines=br.readLine();
}

//reads lines backwards from ArrayList buffer but wanted to read words from end-to-beginning
for(int i = buffer.size()-1; i>=0; i--)
{
System.out.print(buffer.get(i));
}

br.close();
}
}

输出:

installed on their own machine for standalone Java applications, or in a Web browser for Java applets. by a virtual machine (VM) written specifically for the host hardware. End-users commonly use a Java Runtime Environment (JRE) bytecode, instead of directly to platform-specific machine code. Java bytecode instructions are analogous to machine code, but they are intended to be interpreted hardware/operating-system platform. This is achieved by compiling the Java language code to an intermediate representation called Java One characteristic of Java is portability, which means that computer programs written in the Java language must run similarly on any

正如你所看到的,它正在打印从下到上开始的行,但我想要从下到上开始的单词

在这里,我尝试使用带有字符串数组的 StringBuffer,将行按一个空格分割,但在这种情况下,我只能反转每一行,而不能反转整个段落

代码:

import java.io.*;  


public class RevWords4
{

public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\Conor\\Documents\\test.txt"), "UTF-8"));
StringBuffer buffer = new StringBuffer();

String line = br.readLine();
String[] word = line.split(" ");

while(line != null)
{
if(line != null)
{
word = line.split(" ");

for(int i = word.length-1; i>=0; i--)
{
StringBuilder sb = new StringBuilder(word[i] + " ");
buffer.append(sb);
}

buffer.append("\n");
}

line=br.readLine();
}


System.out.print(buffer);
br.close();
}
}

输出:

any on similarly run must language Java the in written programs computer that means which portability, is Java of characteristic One Java called representation intermediate an to code language Java the compiling by achieved is This platform. hardware/operating-system interpreted be to intended are they but code, machine to analogous are instructions bytecode Java code. machine platform-specific to directly of instead bytecode, (JRE) Environment Runtime Java a use commonly End-users hardware. host the for specifically written (VM) machine virtual a by applets. Java for browser Web a in or applications, Java standalone for machine own their on installed

所以我很乐意接受对此的任何建议和提示,谢谢

最佳答案

稍微修改一下 while 循环对我来说就成功了:

StringBuilder str = new StringBuilder();
String[] splitStr = lines.split(" ");
for (int i = splitStr.length; i > 0; i--) {
str.append(splitStr[i - 1]).append(" ");
}

buffer.add(str.toString()); //I wanted to put the words array in here instead of "lines" but didn't know how

所以,你的完整代码如下所示:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;


public class RevWords6
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt"), "UTF-8"));

String lines = br.readLine();
ArrayList<String> buffer = new ArrayList<String>();

//while the data is not null
while(lines != null)
{
//String words[] = lines.split(" ");

//track formatting
if(lines != null)
{
buffer.add("\n");

StringBuilder str = new StringBuilder();
String[] splitStr = lines.split(" ");
for (int i = splitStr.length; i > 0; i--) {
str.append(splitStr[i - 1]).append(" ");
}

buffer.add(str.toString()); //I wanted to put the words array in here instead of "lines" but didn't know how
}

lines=br.readLine();
}

//reads lines backwards from ArrayList buffer but wanted to read words from end-to-beginning
for(int i = buffer.size()-1; i>=0; i--)
{
System.out.print(buffer.get(i));
}

br.close();
}
}
<小时/>

更新使用一个自定义读取器来读取反转的行可能是一个稍微更好的方法:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

public class ReversedLineBufferedReader extends BufferedReader {

public ReversedLineBufferedReader(Reader in) {
super(in);
}

@Override
public String readLine() throws IOException {
String line = super.readLine();

if (line == null) {
return null;
}

StringBuilder reversedLine = new StringBuilder();
if (line.length() > 0) {
String[] splitString = line.split("\\b");
for (int i = splitString.length; i > 0; i--) {
reversedLine.append(splitString[i - 1]).append(" ");
}
}
return reversedLine.toString();
}
}

关于java - 反转整个文本段落而不是反转每个连续行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18944802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com