假设每次输入蒸汽的缓冲区读取1000个字节.缓冲区的开头有一些开始标志和视频名称,在实际视频内容之前,如100字节.我不想将它们写入结果缓冲区.所以第一次将101-999写入缓冲区.而第二次我希望写1000-1999.目前,它再次写入0-999,结果视频在实际视频内容之前有900个额外字节.
无论如何写缓冲区跳过第一个缓冲区长度?谢谢!
代码优先:
AssetManager mgr = DeviceListActivity.this.getApplicationContext().getAssets();
try {
Log.e("Glenn:", address);
FileOutputStream fout = mgr.openFd("device/device_address.txt").createOutputStream();
PrintWriter _fout = new PrintWriter(fout);
_fout.println(address);
Log.e("Glenn", address);
_fout.close();
fout.close();
InputStream fin = mgr.open("device/device_address.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
address = br.readLine();
try {
Log.e("Glenn:", address);
} catch (NullPointerException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
Log.e("Glenn", "error with OutputStream");
}
Run Code Online (Sandbox Code Playgroud)
前两次Log.e()调用打印的地址值是正确的值,实际上是设备MAC地址.但是,当我试图测试从刚刚写入的文件中读取的地址值时,NullPointerException已经在Log.e()调用中捕获了.这意味着从文件中读取的值是NULL.任何人都可以指出代码有什么问题吗?
android inputstream fileoutputstream bufferedreader android-assets
在 java 中,我必须读取多个文件来搜索一些文本。文件包含大量 html 数据,因此很难借助以下代码读取 html 文件的内容。是使用 java 获取文件内容的任何直接方法。我正在使用以下代码,但它使我的应用程序变慢,建议我选择它的最佳替代方案
try{
FileReader fr=new FileReader("path of the html file");
BufferedReader br= new BufferedReader(fr);
String content="";
while((s=br.readLine())!=null)
{
content=content+s;
}
System.out.println("content is"+content);
}
catch(Exception ex)
{
}
Run Code Online (Sandbox Code Playgroud) 我尝试读取当前的cpu频率.经过一些研究,我发现了以下代码:
public String ReadCPUMhz2() throws IOException
{
String[] args = {"/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"};
ProcessBuilder cmd;
cmd = new ProcessBuilder(args);
Process process = null;
process = cmd.start();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line;
Log.d("test","aha");
while ((line = bufferedReader.readLine()) != null) {
log.append(line + "\n");
}
Log.d("test",log.toString());
return log.toString();
}
Run Code Online (Sandbox Code Playgroud)
但那并没有做任何事情.Logcat在while循环之前显示"aha",但"log"似乎是空的.我看不出我做错了什么:s?
PS:抱歉英语不好.
无法通过以下代码从 URL 访问此页面“kissanime.com”的内容(它不返回任何内容):
String a="http://kissanime.com";
url = new URL(a);
URLConnection conn = url.openConnection();
try ( // open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()))) {
String inputLine;
while ((inputLine = br.readLine()) != null) {
System.out.println(inputLine);
}
}
Run Code Online (Sandbox Code Playgroud) 我已经在 stackoverflow 上阅读了一些帖子,还有一些教程,但我没有找到问题的解决方案。
我有一个 Java 客户端,它连接到服务器,然后向服务器发送一行,然后我得到 2 或 3 行作为响应。
这是我的代码:
public static void main(String[] args) {
String message;
String response;
try {
BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
Socket clientSocket = new Socket(hostname, port);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
message = inFromUser.readLine();
outToServer.writeBytes(message + '\n');
// here my program "freezes"
while ((response = inFromServer.readLine()) != null) {
System.out.println("response: " + response);
}
clientSocket.close();
} catch (UnknownHostException e) {
System.out.println("Unknown Host");
} catch …Run Code Online (Sandbox Code Playgroud) 在Java中,我使用标准Scanner和BufferedReader类进行输入,例如:
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
Run Code Online (Sandbox Code Playgroud)
或者
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a=Integer.parseInt(br.readLine());
Run Code Online (Sandbox Code Playgroud)
但是像这样获取输入需要大量的运行时间。我想知道更快的获取输入的方法。提前致谢
编辑:我见过像 uwi 这样的顶级 java 程序员,他们以非常不同的方式接受输入。他们有点创建自己的读者类别。我不明白他们的程序如何在运行时变得更快。
我正在开发一个应用程序,它将手机中的 .txt 文件作为输入并将其打印在 TextView 上,
public class MainActivity extends AppCompatActivity {
Button button;
Intent intent;private StringBuilder text = new StringBuilder();
private InputStream getResources(String s) {
return null;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("text/plain");
startActivityForResult(intent, 7);
Log.v("###", "parent " + getParent());
}
});
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String Fpath = data.getDataString();
// final …Run Code Online (Sandbox Code Playgroud) 一直在努力解决这个问题。长话短说,它不读最后一行的my input。
我想要它做什么...读取文件并退出。
就是这样。它的工作原理上的罚款file ( example: .txt ),但是当涉及到System.in,代码并没有工作!烦人,当它应该很容易。
这是要重新创建的代码和照片!
StringBuilder strBuilder = new StringBuilder();
String line;
// for the last line to "end", I have to do \n! ... but how...?
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while((line = br.readLine()) != null) {
System.out.println(line);
strBuilder.append(line);
}
// Does not reach code
System.out.println(strBuilder.toString());
Run Code Online (Sandbox Code Playgroud)
我知道readLine()在终止\n或\r...在这种情况下,4 fourth line确实不具备后换行,所以我尝试使用!br.isEmpty()或!br.isBlank …
我有这个代码:
public static void opticFiberApplication() throws IOException {
int v1;
File file = new File("src/Arcos.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
int capacity = Integer.parseInt(br.readLine());
Graph application = new Graph(capacity);
while ((v1 = Character.getNumericValue(br.read())) != -1) {
int v2 = Character.getNumericValue(br.read());
int cost = Character.getNumericValue(br.read());
if (application.getVertex(v1) == null) application.addVertex(v1);
if (application.getVertex(v2) == null) application.addVertex(v2);
application.addEdge(v1, v2, cost);
}
printGraph(application);
prim(application);
}
Run Code Online (Sandbox Code Playgroud)
和.txt Arcos
5
0110025032143238
Run Code Online (Sandbox Code Playgroud)
第一行是容量,第二行是顶点和边缘的成本.
我想知道如何使用BufferedReader.readLine().split("")以便可以像这样读取Arcos.txt
5
0 1 10 0 2 5 0 …Run Code Online (Sandbox Code Playgroud) bufferedreader ×10
java ×8
android ×3
bytebuffer ×1
cpu ×1
file-read ×1
frequency ×1
inputstream ×1
sockets ×1
url ×1
video ×1
while-loop ×1