标签: fileinputstream

当我尝试从文件中读取某些字节时,为什么我会得到一个超出范围的索引?

任何人都可以弄清楚为什么我在这里获得索引超出范围的异常?我可以在第一批数据中读取,但是当我尝试再次循环时,它会出错.我也在is.read(readBytes,offset,length)上得到'赋值永不使用'错误; 这也是其中的一部分.

InetAddress address = packet.getAddress();

int port = packet.getPort();

RRQ RRQ = new RRQ();

int offset = 0;
int length = 512;
int block = 0;

byte[] readBytes = new byte[512];

File file = new File(filename);
FileInputStream is = new FileInputStream(file);

int fileBytes = is.read(readBytes, offset, length);

DatagramPacket outPacket = RRQ.doRRQ(readBytes, block, address, port);
socket.send(outPacket);

block++;

if (fileBytes != -1)
{                   
    socket.receive(packet);

    offset = offset + fileBytes;

    System.out.println(offset);

    Exceptions here:fileBytes = is.read(readBytes, offset, length);

    outPacket = RRQ.doRRQ(readBytes, block, address, …
Run Code Online (Sandbox Code Playgroud)

java fileinputstream

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

如何从ImageReader android获取InputStream

我正在看这个课程:https://developer.android.com/reference/android/media/ImageReader.html

我想要的是从ImageReader类中获取一个InputStream,并获取正在渲染到当前表面的图像数据.Current Surface表示当前显示的android屏幕.我在AOSP代码中查看了一个示例,但实际上很难理解.如果有人为我提供了一个很好的ImageReader类示例,那将非常有用.

java android file-descriptor fileinputstream surfaceview

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

如何为输入流类编写Junit

如何为此编写Junit?

当我调试它时,我englishFile在行中得到了Null englishFile = new FileInputStream(fileName);

    String langCode = getLanguageCode();
    String fileName = "appQuestion_"+langCode+".properties";
    FileInputStream englishFile = null;
    try {
        englishFile = new FileInputStream(fileName);
        engProperties.load(englishFile);
    } catch (Exception e) {

    } finally {
        if (null != englishFile) {
            englishFile.close();
        }
    }
Run Code Online (Sandbox Code Playgroud)

java junit fileinputstream

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

这个java代码有什么问题?

我想将txt文件中的整行存储到我设法做到的数组中但是当从数组中搜索时我似乎无法做到这一点.这是代码:

import java.io.*;
import java.util.*;

public class GrandFinal {


public void readFromFile()throws IOException {
    String[] grand = new String[200];
    Scanner search = new Scanner(System.in);
    String query;


    try
    {

        Scanner reader = new Scanner(new FileInputStream("NRLdata.txt"));

        int i = 0;
        while (reader.hasNext()){
            i++;
            grand[i] = reader.next();



        }

        System.out.println("Search for GrandFinal: ");
        query = search.next();
        for(int j = 0; j <grand.length; j++)
        {
            if(grand[i].equals(query)){
                System.out.println (grand[j]);
            }
        }

    reader.close();
    }catch (FileNotFoundException e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
          }
    }


}
Run Code Online (Sandbox Code Playgroud)

它不显示结果

java arrays try-catch fileinputstream

-1
推荐指数
1
解决办法
107
查看次数

Java程序只输出一行代码

我的班级java程序有问题.我必须创建一个程序,提示用户输出文件的路径和名称,该文件将包含我的程序将采用的方程系数,并使用二次公式计算解.到目前为止,我认为一切都是正确的,除了我的输出文件.假设我有一个包含3行系数的输入文件,我的程序将在控制台流中显示解决方案,但只会在我的输出文件中显示1行解决方案.这是我的第一个java课程,这对我来说太过分了!我在这里先向您的帮助表示感谢!

while (input.hasNext()) {

    a = input.nextInt();
    b = input.nextInt();
    c = input.nextInt();

    discriminant = Math.pow(b, 2) - 4 * a * c;
    ///There will be no solutions if discriminant<0
    if (discriminant < 0){
        System.out.println("There are no solutions.");
        output.println("There are no solutions.");
    }
    ///As with the above, if coefficent a = 0 no solutions
    else if (a == 0){
        System.out.println("There are no solutions.");
        output.println("There are no solutions.");
    }
    else if (discriminant == 0){
       solutionOne = (-b + Math.sqrt(discriminant)) / (2 …
Run Code Online (Sandbox Code Playgroud)

java fileinputstream fileoutputstream

-1
推荐指数
1
解决办法
213
查看次数