这是我今天早上在这里提出的一个问题的延伸,所以如果你认为你之前看过这个代码,请不要忽视:D
for (String name : filenames) {
FileInputStream in = new FileInputStream(input.readUTF());
int byteCounter = 0;
int rowCounter = 0;
long bufferCounter = 0;
byte[] b = new byte[10];
int read;
//in.skip(10);
//while((read = in.read()) != -1){
while((read = in.read(b, 0, 10)) != -1){
byteCounter ++;
if (byteCounter != 1000){
if (rowCounter == 1){
System.out.println("\n");
rowCounter = 0;
}
System.out.print(org.apache.commons.codec.binary.Hex.encodeHexString(b));
bufferCounter ++;
rowCounter ++;
}else{
byteCounter = 0;
try{
Thread.sleep(200);
}catch(InterruptedException e) {
}
}
}
System.out.println("\n"+"================"+"\n");
}
Run Code Online (Sandbox Code Playgroud)
嗨,经过几个小时的努力才能得到这个代码,我几乎完成了我正在处理的特定组件.该程序接收一个指定的文件,并应该将该文件的前10个字节转换为Hex.一旦它获取了该文件的前10个字节,它应该停止并移动到下一个指定的文件.目前,它占用整个文件并将其分成多个10字节"块"然后打印出来.换句话说,它在我认为读取的前10个字节之后没有停止(byte [] …
在我的时区晚安.
我正在构建一个http机器人,当我从服务器收到响应时,我想做两件事.首先是打印响应的主体,因为我知道响应的主体是TEXT/HTML类型的我做的第二件事是通过一个html解析器解析响应(在这个特定情况下NekoHtml).代码片段:
//Print the first call
printResponse(urlConnection.getInputStream());
document = new InputSource(urlConnection.getInputStream());
parser.setDocument(document);
Run Code Online (Sandbox Code Playgroud)
问题是当我运行第一行(printResponse)时,第二行将抛出异常.现在问题 - >这是因为InputStream只能被读取一次?每次我们从输入流读取字节被清除?我们怎样才能从输入流中读取更多内容?
提前致谢
最好的祝福
我想做一个简单的TCP客户端.但我收到一个错误.当我做 inputStream = (NSInputStream *)readStream;,outputStream = (NSOutputStream *)writeStream;它建议我引入前缀__bridge或_ bridge _transfer.
首先,它是什么?第二,我试过两个,仍然无法发送消息.我按照本教程,我也发送了消息和流.我安装了Wireshark并且已经调用了send消息,但它没有向ip发送任何数据包.
我刚刚在这里发布了initNetworkCommunication,因为我收到了"网桥"错误.
- (void) initNetworkCommunication {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"54.xxx.xxx.xxx", 1333, &readStream, &writeStream);
inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}
Run Code Online (Sandbox Code Playgroud)
服务器很好,因为我已经尝试了示例代码,我收到了回复.
你能帮助我吗?
我有这个,但是我想知道是否有更快的方法:
URL url=new URL(page);
InputStream is = new BufferedInputStream(url.openConnection().getInputStream());
BufferedReader in=new BufferedReader(new InputStreamReader(is));
String tmp="";
StringBuilder sb=new StringBuilder();
while((tmp=in.readLine())!=null){
sb.append(tmp);
}
Run Code Online (Sandbox Code Playgroud) InputStream,OutputStream和Socket),那么如何通知另一端断开连接?有一种我知道的方法 - 尝试从中读取InputStream,这会抛出IOExceptionif连接关闭,但还有其他方法可以检测到这种情况吗?inputStream.available()
并没有解决这个问题.这是为什么?附加信息:我要求采用另一种方式,因为如果我必须尝试读取InputStrem断开连接,我的项目将变得难以处理
.
我正在尝试运行以下代码并获得IOException:
String cellText = null;
InputStream is = null;
try {
// Find /mydata/myworkbook.xlsx
is = new FileInputStream("/mydata/myworkbook.xlsx");
is.close();
System.out.println("Found the file!");
// Read it in as a workbook and then obtain the "widgets" sheet.
Workbook wb = new XSSFWorkbook(is);
Sheet sheet = wb.getSheet("widgets");
System.out.println("Obtained the widgets sheet!");
// Grab the 2nd row in the sheet (that contains the data we want).
Row row = sheet.getRow(1);
// Grab the 7th cell/col in the row (containing the Plot 500 …Run Code Online (Sandbox Code Playgroud) 我有一个对象,在这个对象中我有一个包含文件的InputStream.
我想将InputStream内部的内容写入文件夹内的文件.
我将如何在Core Java中执行此操作?
我能够使用BufferedReader和.readLine()打印出InputStream的每一行,但是我希望整个文件写入磁盘而不仅仅是内部的文件.
希望这是有道理的,谢谢.
正如标题中所述,我试图使用FileInputSteam逐字节读取文件.我的代码是:
FileInputStream input = new FileInputStream(inFileName);
System.out.println(input.available());
Run Code Online (Sandbox Code Playgroud)
我的文件inFileName只包含字符"±",它应该只有一个字节,但是当我运行程序时,输出为2.
任何帮助是极大的赞赏.
如何使用指定的文件路径而不是资源文件夹中的文件作为输入或输出流?这是我的类,我想从特定的文件路径读取,而不是将txt文件放在IntelliJ的资源文件夹中.输出流相同.任何提供的帮助将不胜感激.
输入流
import java.io.*;
import java.util.*;
public class Example02 {
public static void main(String[] args) throws FileNotFoundException {
// STEP 1: obtain an input stream to the data
// obtain a reference to resource compiled into the project
InputStream is = Example02.class.getResourceAsStream("/file.txt");
// convert to useful form
Scanner in = new Scanner(is);
// STEP 2: do something with the data stream
// read contents
while (in.hasNext()) {
String line = in.nextLine();
System.out.println(line);
}
// STEP 3: be polite, close the …Run Code Online (Sandbox Code Playgroud) 我正在做一些作业,试图使它免受字符输入的影响,但是以某种方式在其中写入字符仍然会破坏字符,因为这会使循环无限。谁能建议一种解决方法?这是代码:
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
srand( time( NULL ) );
int given = 0;
int imaginarynum = ( std::rand() % 99 ) + 1;
int tries = 0;
std::cout << "Hello! We'regoing to play an easy game! I'm goinng to think of a number between 1-100 and you'll have to guess it! " << std::endl;
std::cout << given << " "<< imaginarynum;
while(given < imaginarynum || given > imaginarynum)
{
std::cin >> given;
bool error …Run Code Online (Sandbox Code Playgroud) inputstream ×10
java ×8
file ×3
stream ×2
apache-poi ×1
c++ ×1
cocoa ×1
file-io ×1
hex ×1
ioexception ×1
loops ×1
objective-c ×1
outputstream ×1
sockets ×1
tcpclient ×1
while-loop ×1
xlsx ×1