[英]How can I read a text file and store/print it to the console with only lowercase letters and no spaces?
I am trying to take in a text file (UTF-8) and store/print it with the following constraints (see example below): 我试图接受一个文本文件(UTF-8)并使用以下约束存储/打印它(参见下面的示例):
I first tried using scanner to read until the end of the file. 我首先尝试使用扫描仪读取,直到文件结束。 I converted the string read to a char array but could not make it lowercase correctly (see commented out code).
我将字符串读取转换为char数组但无法正确地使其小写(请参阅注释掉的代码)。 I have tried using BufferedReader, but can't get the correct changes.
我尝试过使用BufferedReader,但无法获得正确的更改。 I tried reading in by character and using the character class to set a lowercase restriction (see code below).
我尝试按字符读取并使用字符类设置小写限制(请参阅下面的代码)。
public static void OpenPlainText(File plainTextFile)
{
try
{
Scanner scanner = new Scanner(plainTextFile);
BufferedReader br = new BufferedReader(new InputStreamReader
(new FileInputStream (plainTextFile), Charset.forName("UTF-8")));
StorePlainText(scanner, br);
scanner.close();
br.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void StorePlainText(Scanner scanner, BufferedReader br) throws IOException
{
/*String str = "";
while (scanner.hasNext())
{
str += scanner.next();
}
str.toCharArray();
str.toLowerCase();
System.out.println(str);*/
int c;
while ((c = br.read()) != -1)
{
char character = (char) c;
if (Character.isLetter(character))
{
if (Character.isUpperCase(character))
{
Character.toLowerCase(character);
System.out.print(character);
}
}
}
}
Ex. 防爆。 Text File
文本文件
Art of Computer Programming, Volume 1, Fascicle 1, The: MMIX -- A RISC Computer for the New Millennium 计算机程序设计艺术,第1卷,分册1,:MMIX - 新千年的RISC计算机
This multivolume work on the analysis of algorithms has long been recognized as the definitive description of classical computer science. 这种关于算法分析的多卷工作一直被认为是对经典计算机科学的权威性描述。
Ex. 防爆。 Store/Print
存储/打印
artofcomputerprogrammingvolumefasciclethemmixarisccomputerforthenewmillenniumthismultivolumeworkontheanalysisofalgorithmshaslongbeenrecognizedasthedefinitivedescriptionofclassicalcomputerscience artofcomputerprogrammingvolumefasciclethemmixarisccomputerforthenewmillenniumthismultivolumeworkontheanalysisofalgorithmshaslongbeenrecognizedasthedefinitivedescriptionofclassicalcomputerscience
Character.toLowerCase
returns the lower case character it does not convert the one you pass in. Character.toLowerCase
返回小写字符,它不会转换您传入的字符。
while ((c = br.read()) != -1)
{
char character = (char) c;
if (Character.isLetter(character))
{
if (Character.isUpperCase(character))
{
character = Character.toLowerCase(character);
System.out.print(character);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.