在我之前的问题中,我提到过,我的OutputStream会做一些奇怪的事情。这已经过去了,但突然又出现了一个疯狂的问题。我不知道,我怎么能读出我的InputStream。到目前为止,我可以为拜德读拜德,但这不是我想要的。我想要一个可变长度的读卡器,我的意思是,如果3字节是发送的,我只想读3字节,如果9字节是发送的,我想读9字节。
这是我的密码:
package com.example.wettkampftimerbt;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URL;
import android.content.Context;
import android.os.AsyncTask;
import android.os.StrictMode;
import android.util.Log;
public class network extends AsyncTask<URL, String, String> {
private final String PREFS_NAME = "prefs", W2 = "w2", W3 = "w3", W4 = "w4",
W5 = "w5", W6 = "w6", SENDBEG = "sendbeg", SENDEND = "sendend";
int w1, w2, w3, w4, w5, w6, w7, w8, sendend, sendbeg, diny1, diny2, diny3,
diny4, diny5, diny6, diny7, diny8, dinymin, dinysek, w90, w91, w92,
w93, w94, w95, w96, w97, netpru, y, retries, length, btw1 = 0,
btw2 = 0, btw3 = 0, btw4 = 0, btw5 = 0, btw6 = 0, btw7 = 0,
btw8 = 0, btw9 = 0, btw10 = 0;
boolean visi1, visi2, visi3, visi4, visi5, visi6, visi7, visi8, login1,
login2, login3, login4, login5, login6, login7, login8, data;
Context context;
private final String LFDNR1 = "lfdnr1";
private final String LFDNR2 = "lfdnr2";
private final String LFDNR3 = "lfdnr3";
private final String LFDNR4 = "lfdnr4";
private final String LFDNR5 = "lfdnr5";
private final String LFDNR6 = "lfdnr6";
private final String LFDNR7 = "lfdnr7";
private final String LFDNR8 = "lfdnr8", COMMAND = "command", LANE = "lane",
DINYMIN = "dinymin", DINYSEK = "dinysek";
Socket sock;
byte[] btw;
byte[] bta;
protected String doInBackground(URL... params) {
retries = 1;
try {
getPrefs(context);
btw1 = sendbeg;
btw2 = w2;// to
btw3 = w3;// from
btw4 = w4;// lfdnr
btw5 = w5;// command
visi(context, false);
SocketAddress sockaddr = new InetSocketAddress("192.168.0.7", 2001);
sock = new Socket();
int timeout = 1000; // 1000 millis = 1 second
sock.connect(sockaddr, timeout);
sock.setReuseAddress(true);
System.out.println(sock);
//from here
if (sock.isConnected()) {
DataOutputStream dos = new DataOutputStream(
(OutputStream) sock.getOutputStream());
DataInputStream dis = new DataInputStream(sock.getInputStream());
BufferedReader rdr = new BufferedReader(new InputStreamReader(dis));
getByte((byte) btw1, (byte) btw2, (byte) btw3, (byte) btw4,
(byte) btw5, (byte) btw6, (byte) btw7, (byte) btw8,
(byte) btw9, (byte) btw10);
dos.write(btw, 0, length);
int in = rdr.read();
dos.flush();
dos.close();
dis.close();
//to here edited
System.out.printf("dis: ", btw);
visi(context, true);
if (diny5 != 32)
System.out.println("ungleich");
reccom();
sock.close();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("IO error " + e + " lulu");
}
return "Done";
}
private byte[] getByte(byte byte1, byte byte2, byte byte3, byte byte4,
byte byte5, byte byte6, byte byte7, byte byte8, byte byte9,
byte byte10) {
if (byte10 > 0) {
btw = new byte[] { byte1, byte2, byte3, byte4, byte5, byte6, byte7,
byte8, byte9, byte10 };
length = 10;
} else if (byte9 > 0 && byte10 == 0) {
btw = new byte[] { byte1, byte2, byte3, byte4, byte5, byte6, byte7,
byte8, byte9 };
length = 9;
} else if (byte8 > 0 && byte9 == 0 && byte10 == 0) {
btw = new byte[] { byte1, byte2, byte3, byte4, byte5, byte6, byte7,
byte8 };
length = 8;
} else if (byte7 > 0 && byte8 == 0 && byte9 == 0 && byte10 == 0) {
btw = new byte[] { byte1, byte2, byte3, byte4, byte5, byte6, byte7 };
length = 7;
} else if (byte6 > 0 && byte7 == 0 && byte8 == 0 && byte9 == 0
&& byte10 == 0) {
btw = new byte[] { byte1, byte2, byte3, byte4, byte5, byte6 };
length = 6;
} else if (byte5 > 0 && byte6 == 0 && byte7 == 0 && byte8 == 0
&& byte9 == 0 && byte10 == 0) {
btw = new byte[] { byte1, byte2, byte3, byte4, byte5 };
length = 5;
} else if (byte4 > 0 && byte5 == 0 && byte6 == 0 && byte7 == 0
&& byte8 == 0 && byte9 == 0 && byte10 == 0) {
btw = new byte[] { byte1, byte2, byte3, byte4 };
length = 4;
} else if (byte3 > 0 && byte4 == 0 && byte5 == 0 && byte6 == 0
&& byte7 == 0 && byte8 == 0 && byte9 == 0 && byte10 == 0) {
btw = new byte[] { byte1, byte2, byte3 };
length = 3;
} else if (byte2 > 0 && byte3 == 0 && byte4 == 0 && byte5 == 0
&& byte6 == 0 && byte7 == 0 && byte8 == 0 && byte9 == 0
&& byte10 == 0) {
btw = new byte[] { byte1, byte2 };
length = 2;
} else if (byte1 > 0 && byte2 == 0 && byte3 == 0 && byte4 == 0
&& byte5 == 0 && byte6 == 0 && byte7 == 0 && byte8 == 0
&& byte9 == 0 && byte10 == 0) {
btw = new byte[] { byte1 };
length = 1;
} else if (byte1 == 0 && byte2 == 0 && byte3 == 0 && byte4 == 0
&& byte5 == 0 && byte6 == 0 && byte7 == 0 && byte8 == 0
&& byte9 == 0 && byte10 == 0) {
btw = new byte[] {};
length = 0;
}
return btw;
}
public void onPreExecute(Context context) {
System.out.println("network_onPreExecute");
getPrefs(context);
}
public void getPrefs(Context context) {
w2 = 0;
w3 = 0;
w4 = 0;
w5 = 0;
w6 = 0;
System.out.println("network_getPrefs");
System.out.println(context);
w2 = Integer.valueOf(context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE).getString(W2, "03"));
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().remove(W2).commit();
w3 = Integer.valueOf(context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE).getString(W3, "00"));
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().remove(W3).commit();
w4 = Integer.valueOf(context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE).getString(W4, "00"));
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().remove(W4).commit();;
w5 = Integer.valueOf(context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE).getString(W5, "49"));
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().remove(W5).commit();
w6 = Integer.valueOf(context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE).getString(W6, "00"));
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().remove(W6).commit();
sendbeg = Integer.valueOf(context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE).getString(SENDBEG, "00"));
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().remove(SENDBEG).commit();
sendend = Integer.valueOf(context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE).getString(SENDEND, "00"));
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().remove(SENDEND).commit();
}
public network(Context context) {
System.out.println("network_Context");
onPreExecute(context);
this.context = context;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
}我甚至感谢代码优化-提示。
发布于 2013-02-04 10:43:35
try(FileInputStream in = new FileInputStream(accountFile);
BufferedReader rdr = new BufferedReader(new InputStreamReader(in))
)
{
String in = rdr.readLine();
}这是我的项目的一部分,就是做你想做的事情。
发布于 2013-02-04 11:44:02
您要寻找的方法是DataInputStream.readFully().。
https://stackoverflow.com/questions/14684961
复制相似问题