标签: inputstream

InputStream read(byte [] b,int off,int len) - 删除文件的其余部分?

这是我今天早上在这里提出的一个问题的延伸,所以如果你认为你之前看过这个代码,请不要忽视: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 [] …

java file-io hex inputstream file

0
推荐指数
1
解决办法
3825
查看次数

InputStream读取

在我的时区晚安.

我正在构建一个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只能被读取一次?每次我们从输入流读取字节被清除?我们怎样才能从输入流中读取更多内容?

提前致谢

最好的祝福

java inputstream

0
推荐指数
1
解决办法
2178
查看次数

可可,免费桥接和ARC

我想做一个简单的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)

服务器很好,因为我已经尝试了示例代码,我收到了回复.

你能帮助我吗?

cocoa inputstream objective-c stream tcpclient

0
推荐指数
1
解决办法
1042
查看次数

使用Java获取HTML内容的最快方法是什么?

我有这个,但是我想知道是否有更快的方法:

        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)

java inputstream bufferedinputstream

0
推荐指数
1
解决办法
2874
查看次数

JAVA:处理插座断开连接

  1. 两台计算机通过套接字连接.如果服务器/客户端从它们的末端关闭连接(即关闭InputStream,OutputStreamSocket),那么如何通知另一端断开连接?有一种我知道的方法 - 尝试从中读取InputStream,这会抛出IOExceptionif连接关闭,但还有其他方法可以检测到这种情况吗?
  2. 另一个问题,我在互联网上看了问题,看到inputStream.available() 并没有解决这个问题.这是为什么?

附加信息:我要求采用另一种方式,因为如果我必须尝试读取InputStrem断开连接,我的项目将变得难以处理 .

java sockets inputstream outputstream stream

0
推荐指数
1
解决办法
1万
查看次数

Apache POI在读取XLSX工作簿时抛出IOException

我正在尝试运行以下代码并获得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)

java inputstream xlsx ioexception apache-poi

0
推荐指数
1
解决办法
5145
查看次数

将InputStream写入文件

我有一个对象,在这个对象中我有一个包含文件的InputStream.

我想将InputStream内部的内容写入文件夹内的文件.

我将如何在Core Java中执行此操作?

我能够使用BufferedReader和.readLine()打印出InputStream的每一行,但是我希望整个文件写入磁盘而不仅仅是内部的文件.

希望这是有道理的,谢谢.

java inputstream file

0
推荐指数
1
解决办法
4653
查看次数

当文件中只有1个字节时,FileInputStream可用方法返回2

正如标题中所述,我试图使用FileInputSteam逐字节读取文件.我的代码是:

FileInputStream input = new FileInputStream(inFileName);
System.out.println(input.available());
Run Code Online (Sandbox Code Playgroud)

我的文件inFileName只包含字符"±",它应该只有一个字节,但是当我运行程序时,输出为2.

任何帮助是极大的赞赏.

java inputstream file

0
推荐指数
1
解决办法
93
查看次数

来自文件路径的Java输入和输出流

如何使用指定的文件路径而不是资源文件夹中的文件作为输入或输出流?这是我的类,我想从特定的文件路径读取,而不是将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)

java inputstream

0
推荐指数
1
解决办法
5255
查看次数

为什么循环不断?

我正在做一些作业,试图使它免受字符输入的影响,但是以某种方式在其中写入字符仍然会破坏字符,因为这会使循环无限。谁能建议一种解决方法?这是代码:

#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)

c++ loops inputstream infinite-loop while-loop

0
推荐指数
1
解决办法
100
查看次数