我试图从txt文件(书)中读取,然后将其每一行添加到链表.但是,当我运行代码时,我得到了一个outofmemory错误l.add(line);.你能告诉我这段代码我做错了什么吗?或者,有没有更好的方法来存储String值而不是LinkedList?
非常感谢!
public Book (String bookname) throws java.io.IOException{
f = new FileReader(bookname);
b = new BufferedReader(f);
l = new LinkedList<String>();
String line = b.readLine();
while (line != null) {
l.add(line);
}
b.close();
}
Run Code Online (Sandbox Code Playgroud) 我只是想用Java执行一个进程,所以
Runtime runtime = Runtime.getRuntime();
this.process = null;
try {
this.process = runtime.exec(new String[] {
properties.getPropertyStr("ffmpegExecutable", "/usr/bin/ffmpeg"),
"-i", this.streamEntry.getSource(),
"-vcodec", "copy",
"-acodec", "copy",
this.streamEntry.getDestination()
});
} catch (IOException e) {
e.printStackTrace();
return;
}
BufferedReader stdout = new BufferedReader(???process.getOutputStream());
Run Code Online (Sandbox Code Playgroud)
我只是希望能够逐行读取过程的输出.我该怎么做呢?
我有这样的代码:
BufferedReader in = new BufferedReader(new FileReader("C:\file.txt"));
String text = in.readLine();//lets say text is now "asd"
Run Code Online (Sandbox Code Playgroud)
之后我有了一个方法:
private static void doSomething(Enum word){
...
}
Run Code Online (Sandbox Code Playgroud)
是否有可能以某种方式将此文本转换为枚举?
我正在尝试阅读URL的内容.BufferedReader的初始化会导致崩溃.
我试过查找人们的解决方案,但无论我做什么,我都会遇到这个错误.
这是我的代码:
try {
String sUrl = "http://www.google.com";
System.out.println("About to create URL.");
URL url = new URL(sUrl);
System.out.println("Created URL");
System.out.println("About to create URLConnection");
URLConnection urlc = url.openConnection();
System.out.println("Created URLConnection");
System.out.println("About to create the BufferedReader");
BufferedReader bR = new BufferedReader(new
InputStreamReader(urlc.getInputStream()));
System.out.println("Created the BufferedReader");
System.out.println("About to create a StringBuilder");
StringBuilder sBuilder = new StringBuilder();
System.out.println("Created StringBuilder");
int byteRead;
System.out.println("About to enter while loop and build string");
while ((byteRead = bR.read()) != -1)
{
sBuilder.append((char) byteRead);
}
System.out.println("Built string");
bR.close();
System.out.println("Buffered …Run Code Online (Sandbox Code Playgroud) 我试图从格式如下的文件中提取信息:
1
test@mail.ca|password|false
Run Code Online (Sandbox Code Playgroud)
但是,我似乎ArrayIndexOutOfBounds在运行以下代码时遇到错误,我无法确定原因,因为我认为我的分裂应该正常运行.在以"users"开头的行上获得错误.
sc = new Scanner (System.in);
BufferedReader in = new BufferedReader (new FileReader (USAVE));
int repeats = Integer.parseInt(in.readLine());
for (int c = 0; c < repeats; c++){
String info = in.readLine();
System.out.println (info);
String[] extracted = info.split("\\|");
users.addUser(extracted[0], decryptPassword(extracted[1]));
}
in.close();
Run Code Online (Sandbox Code Playgroud)
可能是什么问题呢?
编辑:我改变了"|" 到"\ |" 但问题仍然存在.
EDIT2:StackTrace
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at OnlineCommunications.userFromFile(OnlineCommunications.java:165)
at OnlineCommunications.logIn(OnlineCommunications.java:36)
at OnlineCommunications.emailOption(OnlineCommunications.java:593)
at OnlineCommunications.main(OnlineCommunications.java:683)
Run Code Online (Sandbox Code Playgroud)
我上面发布的方法是名为userFromFile.
我试图使用BufferedReader从输入中读取a.它第一次工作,但第二次运行我得到一个例外.
john@fekete:~/devel/java/pricecalc$ java frontend.CUI
> gsdfgd
Invalid command!
> I/O Error getting string: java.io.IOException: Stream closed
I/O Error: java.io.IOException: java.io.IOException: Stream closed
> I/O Error getting string: java.io.IOException: Stream closed
I/O Error: java.io.IOException: java.io.IOException: Stream closed
> I/O Error getting string: java.io.IOException: Stream closed
Run Code Online (Sandbox Code Playgroud)
它只是在循环中继续运行.我一定错过了什么.
public static void main(String args[]) {
if (args.length == 0) {
while (!exit) {
try {
exit = processLine(commandLine());
} catch (IOException e) {
System.out.println("I/O Error: " + e);
}
}
System.out.println("Bye!");
} else if …Run Code Online (Sandbox Code Playgroud) 我想阅读.txt行以完成任务,然后移至下一行。
为了使程序更快,我想使用多线程,因此每个线程都尝试一行。
问题是,我不希望线程1与线程2尝试同一行。
有任何想法吗?
我正在尝试学习如何使用文本文件中的逐行读取.即使我将txt文件放在同一个src中,控制台也始终将错误显示为 - No such file or directory.
public class ddd {
public static void main(String[] args) {
FileInputStream fis = null;
BufferedReader reader = null;
try {
fis = new FileInputStream("/dd/src/com/dd/input.txt");
reader = new BufferedReader(new InputStreamReader(fis));
System.out
.println("Reading File line by line using BufferedReader");
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
reader.close();
fis.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
} …Run Code Online (Sandbox Code Playgroud) 一直在寻找解决此问题的方法。阅读所有先前的答案,但没有一个帮助我。SonarQube可能有任何错误吗?
public class Br {
public String loader(String FilePath){
BufferedReader br;
String str = null;
StringBuilder strb = new StringBuilder();
try {
br = new BufferedReader(new FileReader(FilePath));
while ((str = br.readLine()) != null) {
strb.append(str).append("\n");
}
} catch (FileNotFoundException f){
System.out.println(FilePath+" does not exist");
return null;
} catch (IOException e) {
e.printStackTrace();
}
return strb.toString();
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试平均一个包含1000万个数字的文件,每行只有一个数字。我创建了一个名为getAverage()的方法,该方法返回一个double。这是代码:
public double promedioDouble() throws IOException
{
double adder = 0;
int counter = 0;
try{
file = new FileReader(filePath);
reader = new BufferedReader(file);
while(reader.readLine()!=null)
{
adder+=Double.parseDouble(reader.readLine());
counter++;
}
}
catch (IOException e)
{
System.out.println("cant read");
}
finally {
if (file != null) file.close();
}
return adder/counter;
}
Run Code Online (Sandbox Code Playgroud)
我打印了计数器,结果显示为5.000.000,我不知道为什么读者无法读取文件中包含的1000万个数字,而它只能读取一半。我需要帮助。
bufferedreader ×10
java ×10
filereader ×2
android ×1
arrays ×1
enums ×1
file-io ×1
input ×1
linked-list ×1
try-catch ×1