我知道将 BufferedInpurStream 包裹在 FileInputStream 周围可以使读取操作更快,但试图弄清楚如何实现。我查看了 BufferedInpurStream 的源代码并得到了一些东西。这是我的理解
InputStream in = new BufferedInpurStream(new FileInputStream(filename));
int byteToRead;
while((byteToRead = in.read()) != -1)
Run Code Online (Sandbox Code Playgroud)
当我执行 bufferedInpurStream.read() 时,在内部,它会首先一次将字节块读取到缓冲区中,然后从缓冲区中一个一个地读取每个字节,而不是从文件中读取它(这会更昂贵)。一旦该缓冲区是空的,它将再次用字节块填充它
而使用 FileInputStream.read() 时,从文件中逐个字节执行读取操作,这是非常昂贵的
这种理解正确吗?
我需要从Java中的InputStream读取零终止字符串.
是否有类似于BufferedReader.readLine()方法来读取以零结尾的字符串?
如何确定Buffer的大小,同时使用Buffered Input Stream读取批量文件?它是基于文件大小吗?我正在使用,
byte[] buf = new byte[4096];
Run Code Online (Sandbox Code Playgroud)
如果我增加缓冲区大小,它会快速读取?
我也主要使用Scanner,也想尝试使用缓冲读卡器:到目前为止我所拥有的
import java.util.*;
import java.io.*;
public class IceCreamCone
{
// variables
String flavour;
int numScoops;
Scanner flavourIceCream = new Scanner(System.in);
// constructor
public IceCreamCone()
{
}
// methods
public String getFlavour() throws IOexception
{
try{
BufferedReader keyboardInput;
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" please enter your flavour ice cream");
flavour = keyboardInput.readLine();
return keyboardInput.readLine();
}
catch (IOexception e)
{
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我相当肯定会得到一个你可以说的int
Integer.parseInt(keyboardInput.readLine());
Run Code Online (Sandbox Code Playgroud)
但如果我想要一个字符串怎么办?
我试图用膨胀的视图填充Android GridView,视图具有从ArrayList的数据填充的ImageView和TextView.
一切都很好但是,当我滚动网格时,我的前7个项目正在重复.
namCont.setAdapter(new ImageAdapter(getApplicationContext()));
Run Code Online (Sandbox Code Playgroud)
我的代码:
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
public ImageAdapter(Context c)
{
mContext = c;
}
public int getCount()
{
return kat.namirnice.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View view;
ImageView imageView = null;
if (convertView == null)
{
view = LayoutInflater.from(mContext).inflate(R.layout.nam_item,null);
try
{
TextView textView = (TextView)view.findViewById(R.id.tekst);
imageView = (ImageView)view.findViewById(R.id.slika);
textView.setText(kat.namirnice.get(position).naziv);
Log.i(TAG, …Run Code Online (Sandbox Code Playgroud) 使用Java 7和SSL套接字时,我遇到了一个令人费解的问题.
我有一个客户端/服务器包.两者都使用非常简单的协议相互发送XML数据.每条消息的前4个字节始终包含整个消息的长度.这意味着,它是XML数据的长度加上4个字节.
首先,客户端向服务器发送问候消息.接下来,服务器解释问候语并发送响应.然后,客户端自己解释该消息并发送其登录信息.服务器检查该信息并发回响应.但是这一次,客户端没有收到任何东西,尽管它使用与从服务器获取问候响应时相同的方法.
这是客户端的简化阅读方法:
private String readResponse() throws Exception
{
BufferedInputStream inputBuffer = new BufferedInputStream(sslSocket.getInputStream());
// Read and interprete the first 4 bytes to get the length of the XML-data
int messageLength = readMessageLength(inputBuffer) - 4;
// PROBLEM OCCURS HERE!
// Read bufferSize bytes from stream
byte[] byteArray = new byte[messageLength];
inputBuffer.read(byteArray);
// Return the XML-data
return new String(byteArray);
}
Run Code Online (Sandbox Code Playgroud)
这里是从前4个字节中检索长度的方法......
private int readMessageLength(BufferedInputStream in) throws IOException {
byte[] b = new byte[4];
in.read(b, 0, 4);
return (((b[0] & …Run Code Online (Sandbox Code Playgroud) 以下代码使用 BufferedInputStream 从文件中读取数据并按块进行处理。我想修改此代码,以便数据不是通过流来自文件,而是来自字节数组。我已经将文件的数据读入字节数组,并且我想使用这个 while...循环来处理数组数据,而不是使用文件流中的数据。不知道该怎么做:
FileInputStream in = new FileInputStream(inputFile);
BufferedInputStream origin = new BufferedInputStream(in, BUFFER);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1)
{
// Do something
}
Run Code Online (Sandbox Code Playgroud) 我需要从服务器接收320字节的数据,该服务器包含80个4字节的int字段.我如何以4的字节接收它们并显示它们各自的int值?谢谢.
不确定这是否适合接收部分:
//for reading the data from the socket
BufferedInputStream bufferinput=new BufferedInputStream(NewSocket.getInputStream());
DataInputStream datainput=new DataInputStream(bufferinput);
byte[] handsize=new byte[32];
// The control will halt at the below statement till all the 32 bytes are not read from the socket.
datainput.readFully(handsize);
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) 我从MimeMessage获得输入流.在InputStream中,最后我要添加\ r \n.\ r \n
表示消息的结尾.
请建议.
我正在尝试使用我的Android应用程序HttpUrlConnection中的网络读取文件AsyncTask.
虽然我注意到文件下载工作的速度比它应该有的慢一点,因为我使用的网络速度更快.
所以我检查并发现该BufferedInputStream对象一次最多只能读取2048个字节.没有我设置的缓冲区大小.甚至内部默认缓冲区大小为BufferedInputStream8192字节.
我在这里添加我的代码以供参考.
private class DownloadFileTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedInputStream input = null;
OutputStream output = null;
int lengthOfFile;
int totalBytesDownloaded = 0;
int count;
final int bufferSize = 8 * 1024; // 8KB
try {
// Create the URL
URL url = new URL(params[0]);
// Open connection
connection = (HttpURLConnection) url.openConnection();
// Get the file length
lengthOfFile = …Run Code Online (Sandbox Code Playgroud)