对于在学校的作业我被要求创建一个简单的程序,创建1000个文本文件,每个文件具有随机数量的行,通过多线程\单个进程计算有多少行.而不是删除那些文件.
现在测试过程中发生了一件奇怪的事情 - 所有文件的线性计数总是比以多线程方式计算它们要快一点,这已经激发了我课堂圈内的学术理论化课程.
当Scanner用于读取所有文件时,一切都按预期工作 - 在500毫秒线性时间和400毫秒线程时间读取1000个文件
然而,当我使用BufferedReader时间下降到大约110ms线性和130ms线程.
哪部分代码会导致这个瓶颈?为什么?
编辑:只是为了澄清,我不是问为什么Scanner工作慢于BufferedReader.
完整的可编译代码:(虽然你应该改变文件创建路径输出)
import java.io.*;
import java.util.Random;
import java.util.Scanner;
/**
* Builds text files with random amount of lines and counts them with
* one process or multi-threading.
* @author Hazir
*/// CLASS MATALA_4A START:
public class Matala_4A {
/* Finals: */
private static final String MSG = "Hello World";
/* Privates: */
private static int count;
private static Random rand;
/* Private Methods: …Run Code Online (Sandbox Code Playgroud) 试图拖尾/解析一些日志文件.条目以日期开头,然后可以跨越多行.
这有效,但是没有看到要提交的新条目.
File inputFile = new File("C:/test.txt");
InputStream is = new FileInputStream(inputFile);
InputStream bis = new BufferedInputStream(is);
//bis.skip(inputFile.length());
Scanner src = new Scanner(bis);
src.useDelimiter("\n2010-05-01 ");
while (true) {
while(src.hasNext()){
System.out.println("[ " + src.next() + " ]");
}
}
Run Code Online (Sandbox Code Playgroud)
看起来不像Scanner的next()或hasNext()检测到新的文件条目.
任何想法我还能实现,基本上,一个带有自定义分隔符的tail -f.
好的 - 使用凯利的建议我正在检查和刷新扫描仪,这是有效的.谢谢 !!
如果有人有改进建议PLZ吗!
File inputFile = new File("C:/test.txt");
InputStream is = new FileInputStream(inputFile);
InputStream bis = new BufferedInputStream(is);
//bis.skip(inputFile.length());
Scanner src = new Scanner(bis);
src.useDelimiter("\n2010-05-01 ");
while (true) {
while(src.hasNext()){
System.out.println("[ " + src.next() + " ]"); …Run Code Online (Sandbox Code Playgroud) 我试图理解的java文件中的一行是如下所示.
return new Scanner(file).useDelimiter("\\Z").next();
Run Code Online (Sandbox Code Playgroud)
根据java.util.regex.Pattern文档,该文件应返回"输入的结尾,但对于最终的终结符,如果有的话".但是,它只返回文件中的前1024个字符.这是正则表达式模式匹配器施加的限制吗?这可以克服吗?目前我正在使用文件阅读器.但我想知道这种行为的原因.
我正在尝试编写一个程序,将字母表中的任何字母(大写或小写)切换为Phontic字母表.例如,如果我输入"A"或"a",我的程序将给我(更改为)"Alpha".我已经对这个和切换语句做了很多研究,但我一直陷入困境.我意识到我不能在扫描仪中使用'char'.但是,当我将'char'更改为'String'时,我的switch语句会混乱(特别是我的代码中的toUpperCase被加下划线.我看不出我的错误.这是我到目前为止所做的:
import java.util.Scanner;
public class PhoneticTranslate {
public static void main(String[] args) {
char letter;
String phonetic;
Scanner kb = new Scanner(System.in);
System.out.print("Please enter a letter: ");
letter = kb.next();
switch(Character.toUpperCase(letter))
{
case 'A':
phonetic = "Alpha";
break;
case 'B':
phonetic = "Bravo";
break;
case 'C':
phonetic = "Charlie";
break;
case 'D':
phonetic = "Delta";
break;
case 'E':
phonetic = "Echo";
break;
case 'F':
phonetic = "Foxtrot";
break;
case 'G':
phonetic = "Golf";
break;
case 'H':
phonetic = …Run Code Online (Sandbox Code Playgroud) 使用Scanner时遇到UTF-8编码问题.我的数据文件的示例两行:
000001 M?lynas Tadas 63210309683 V 2003/03/17 2016/03/17
000002 Raudonas Tomas 65505023282 V 2006/01/26 2018/01/26
Run Code Online (Sandbox Code Playgroud)
目前我使用Scanner单独读取文本而不是整行,因为这样更方便,但由于编码,它无法正确读取.我已经阅读了有关使用InputStream等的内容,但我不想处理凌乱的线路斩波.有没有办法使用UTF-8 扫描仪?
我有一个使用多个类的程序,我希望其他类能够访问我在主类中声明的相同扫描程序,我假设它将使用某种get方法完成,但我无法找到任何资源来帮助我.
以下是我在主要课程中制作的扫描仪:
Scanner in = new Scanner(System.in);
System.out.println("Enter a filename");
String filename = in.nextLine();
File InputFile = new File (filename);
Scanner reader = new Scanner(filename);
Run Code Online (Sandbox Code Playgroud)
读者Scanner是我希望能够访问组成该程序的其他类的人,任何人都可以给我一些指导我如何做到这一点?非常感谢您的帮助!
我有几个文件(实际上它们也是保存在 Ubuntu 上的 Eclipse 中的 java 源文件),我需要逐行读取和处理它们。我注意到我无法读取其中一个文件。我使用的代码如下
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine() ) {
builder.append(scanner.nextLine()).append("\n");
}
} catch (FileNotFoundException ex) {
System.out.println("Error");
}
Run Code Online (Sandbox Code Playgroud)
我事先检查了文件是否存在。确实如此。我什至可以重命名它。但我不能读一行。hasNextLine 只返回 false。(我什至尝试过 hasNext)。
最后我看了一下文件的内容,发现有一个不同的字符(在java文件的注释部分)。它是以下字符。
¸
Run Code Online (Sandbox Code Playgroud)
当我删除这个字符时,我可以正常读取文件。然而这是不可接受的。即使其中包含该字符,我该怎么做才能读取文件?
所以我有一个文件,其中包含所有的总统 - 他们的名字,中间名首字母(如果有的话)和姓氏.
需要读入该文件,并且用户可以输入总统的名称来搜索它,并且应该显示该总统.
如果用户按名字或姓氏搜索,而不是两者都搜索,我会显示总统.
例如,外部文件包含:
George,Washington,(1789-1797)
Franklin,D.,Roosevelt,(1933-1945)
... and so on with all the presidents
Run Code Online (Sandbox Code Playgroud)
我需要用户能够键入名字,姓氏,或者姓名和名字,并获得所需的结果(日期与大部分时间无关).
试过很多不同的事情,但如果用户按名字和姓氏搜索,就不能到达显示总统的地方.
这是我到目前为止所得到的:
public class NameSearch {
public static void main(String[] args) throws IOException {
try {
// read from presidents file
Scanner presidentsFile = new Scanner(new File("Presidents.txt"));
// scanner for user input
Scanner keyboard = new Scanner(System.in);
// create array list of each line in presidents file
ArrayList<String> presidentsArrayList = new ArrayList<String>();
// prompt user to enter a string to see if it …Run Code Online (Sandbox Code Playgroud) 我正在编写一个 Java 应用程序,它同时使用Scanner和Console来获取用户的输入。我想添加命令历史支持,以便用户可以使用箭头键搜索以前的输入(类似于终端)。有没有办法做到这一点?现在,当我使用 theScanner或 the 时Console,我会得到奇怪的符号,就像^[[A按箭头键一样。
我已经阅读了KeyListener和KeyEvent,但我的应用程序不使用 GUI。
谢谢!
我无法让我的程序响应空输入.例如,假设我想提示用户输入货币类型BigDecimal以及货币类型.这是有问题的程序的样子.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount of money "
+ "and specify currency (USD or CNY): ");
// initialize variable moneyInput
BigDecimal moneyInput;
// check if moneyInput is numeric
try {
moneyInput = input.nextBigDecimal();
} catch (InputMismatchException e){
System.err.println("InputMismatchException: non-numeric value");
return;
}
// specify currency of moneyInput
String currencyType = input.next();
...
}
Run Code Online (Sandbox Code Playgroud)
所以在这里,输入像100.00 USD,工作正常.
Enter the amount of money and specify currency (USD or CNY): …Run Code Online (Sandbox Code Playgroud)