某些编程语言中的常见模式是有一个函数,在调用时返回下一个值,直到到达有限序列的末尾,在这种情况下,它会一直返回 null。
Java 中的一个常见示例是这样的:
void printAll(BufferedReader reader) {
String line;
// Assigns readLine value to line, and then check if not null
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
Run Code Online (Sandbox Code Playgroud)
它与Iterator 设计模式iterator中的 类似,但迭代器有 a和 a ,而BufferedReader没有检查功能,只有 形式,其中返回的对象可以为 null 以标记序列的结束。我调用诸如“下一个函数”(或者可能是“ yield ”函数)之类的函数,但我不知道是否有一个词来形容这种模式。next(): ObjecthasNext(): BooleanhasNext()next(): Object?next()
在 Java 中,表达式可以包含赋值,这允许如下构造:(line = reader.readLine()) != null。此代码将可为空的值赋给readLine()to line,然后检查 in 的值是否line不为空。但 Kotlin 不允许这样的构造,因为在 Kotlin 中,赋值不是表达式,因此它不能用作 …
我正在尝试编写一个方法来获取多行制表符分隔文件并将该文件的内容作为String数组的arraylist返回(每行是一个String [],每个这样的String []是一个arraylist的元素).我的问题是,我无法判断输出是否正确.我已经打印了每个arraylist元素和String []元素,因为它们被保存到arraylist,并且这些打印看起来是正确的.但是在返回arraylist并在其中打印String []后,它们似乎只包含文件最后一行的内容.我怀疑它可能是我不知道的FileReader或BufferedReader.Anyhoo,这是代码:
public class DataParsingTest {
static File AAPLDailyFile = new File("./textFilesForMethodTests/dataParsingPractice2.tsv");
public static void main(String[] args) throws FileNotFoundException, IOException {
ArrayList<String[]> stringArrayList = fileToStringArray(AAPLDailyFile);
System.out.println("stringArray.size() = " + stringArrayList.size());
System.out.println(stringArrayList.get(0)[0]);
for (int i = 0; i < stringArrayList.size(); i++) {
for (int j = 0; j < stringArrayList.get(i).length; j++) {
System.out.println("index of arraylist is " + i + " and element at index " + j + " of that array is " + stringArrayList.get(i)[j]);
} …Run Code Online (Sandbox Code Playgroud) 我在res/raw目录中保存了一些资源,然后我想用自定义加载器读取.
我怎样才能做到这一点?
理想情况下,我会得到一个BufferedReader.
谢谢!
我有一个在Python 2和Python 3中运行的程序,但速度有很大差异.我理解在交换机中进行了一些内部更改,但io.BufferedReader的差异非常大.在这两个版本中,我都使用io.BufferedReader,因为主程序循环一次只需要一个字节的数据.以下是脚本的cProfile输出的摘录(请参阅cumtime,而不是tottime):
Python 2:
ncalls tottime percall cumtime percall filename:lineno(function)
36984 0.188 0.000 0.545 0.000 io.py:929(read)
Python 3:
36996 0.063 0.000 0.063 0.000 {method 'read' of '_io.BufferedReader' objects}
Run Code Online (Sandbox Code Playgroud)
当我打印对象时,两者都返回类似的东西io.BufferedReader,我确信它们都使用BufferedReader.
这是有问题的代码.见第28行.调用者负责设置bufstream.我用了bufstream = io.open('testfile', 'rb')
为什么BufferedReader的速度在读取文件中的单个字节方面存在如此巨大的差异,以及如何"修复"Python 2.x的问题?我正在运行Python 2.6和Python 3.1.
我知道我在这里做了一些非常错误的事情,但我会坦白地说,我对java的了解非常薄弱.每当我调用dataIn.readLine()时,我都会收到此编译时错误
unreported exception java.io.IOException; must be caught or declared to be thrown
Run Code Online (Sandbox Code Playgroud)
这是代码,我知道命名约定很糟糕,它几乎什么都不做.
import java.io.*;
public class money {
public static void main( String[]args ){
String quarters;
String dimes;
String nickels;
String pennies;
int iquarters;
int idimes;
int inickels;
int ipennies;
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
System.out.println( "Enter the number of quarters. " );
quarters = dataIn.readLine();
System.out.println( "Enter the number of dimes" );
dimes = dataIn.readLine();
System.out.println( "Enter the number of nickels" );
nickels = dataIn.readLine();
System.out.println( "Enter …Run Code Online (Sandbox Code Playgroud) 我怎样才能获得BufferedWriter一个BufferedReader?
我希望能够做到这样的事情:
BufferedReader read = new BufferedReader(new InputStreamReader(...));
BufferedWriter write = new BufferedWriter(read);
Run Code Online (Sandbox Code Playgroud) 我想在Android ICS中读取/ proc/net/xt_qtaguid/stats,它记录所有接口和应用程序流量统计信息.以下是代码段:
String line = null;
BufferReader reader = new BufferedReader(new FileReader(new File("/proc/net/xt_qtaguid/stats")));
line = reader.readLine();/*Here I can read the 1st line correctly, it return "idx iface acct_tag_hex..."*/
splitLine(line, keys);
line = reader.readLine();//!!!!!Read next line, it returns null!!!!!!
Run Code Online (Sandbox Code Playgroud)
如果我捕获此文件,它将显示:
idx iface acct_tag_hex uid_tag_int cnt_set rx_bytes rx_packets tx_bytes tx_packets rx_tcp_bytes rx_tcp_packets rx_udp_bytes rx_udp_packets rx_other_bytes rx_other_packets tx_tcp_bytes tx_tcp_packets tx_udp_bytes tx_udp_packets tx_other_bytes tx_other_packets
2 rmnet0 0x0 0 0 6266 105 8882 121 1428 30 4838 75 0 0 208 4 2552 44 …
我目前在使用OutputStream的客户端 - 服务器应用程序中使用Java套接字而不是BufferedOutputStream(对于输入流也是如此).
客户端和服务器交换序列化对象(writeObject()方法).
在这种情况下使用BufferedOutputStream和BufferedInputStream是否有意义(更快)?
当我必须刷新或者我不应该写一个flush()语句?
我正在使用Java 8 Streams从csv文件创建流.我正在使用BufferedReader.lines(),我阅读了以下文档BufferedReader.lines():
在执行终端流操作之后,不能保证读取器将处于从中读取下一个字符或行的特定位置.
public class Streamy {
public static void main(String args[]) {
Reader reader = null;
BufferedReader breader = null;
try {
reader = new FileReader("refined.csv");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
breader = new BufferedReader(reader);
long l1 = breader.lines().count();
System.out.println("Line Count " + l1); // this works correctly
long l2 = breader.lines().count();
System.out.println("Line Count " + l2); // this gives 0
}
}
Run Code Online (Sandbox Code Playgroud)
它看起来像是第一次读取文件后,读者无法进入文件的开头.解决这个问题的方法是什么?
这是我的代码:
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
public class SymbolBalance{
public static void main(String[] args) throws Exception{
File givenFile = null;
String words = null;
if(args.length > 0){
givenFile = new File(args[0]);
} else{
System.out.println("Error! No file name was given!");
}
BufferedReader scan = new BufferedReader(new FileReader(givenFile));
while(words = scan.readLine() != null){
System.out.println(words);
}
scan.close();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的错误:
codio@titanic-avenue:~/workspace$ javac SymbolBalance.java
SymbolBalance.java:21: error: incompatible types: boolean cannot
be converted to String
while(words = scan.readLine() != null){
^
SymbolBalance.java:21: error: incompatible …Run Code Online (Sandbox Code Playgroud) bufferedreader ×10
java ×6
filereader ×3
android ×2
ioexception ×1
iostream ×1
iteration ×1
iterator ×1
java-8 ×1
java-stream ×1
kotlin ×1
loading ×1
performance ×1
proc ×1
python ×1
python-3.x ×1
sockets ×1
while-loop ×1