我希望我的程序将URL地址(一次一个)保存到文件中.这些地址需要以UTF格式保存,以确保它们是正确的.
我的问题是文件一直被覆盖,而不是附加:
DataOutputStream DOS = new DataOutputStream(new FileOutputStream(filen, true));
Count = 0;
if (LinkToCheck != null) {
System.out.println(System.currentTimeMillis() + " SaveURL_ToRelatedURLS d "+LinkToCheck.Get_SelfRelationValue()+" vs "+Class_Controller.InterestBorder);
if (LinkToCheck.Get_SelfRelationValue() > Class_Controller.InterestBorder) {
DOS.writeUTF(LinkToCheck.Get_URL().toString() + "\n");
Count++;
}
}
DOS.close();
Run Code Online (Sandbox Code Playgroud)
此代码不会附加,所以我该如何追加它?
我有以下代码生成OutOfMemory异常:
byte[] buf = new byte[10240];
int len = 0;
DataOutputStream dataOS = new DataOutputStream(conn.getOutputStream());
while ((len = _inputStream.read(buf)) > 0) {
System.out.println("len : " + len);
System.out.println("Going to write buf into dataOS : " + buf.length);
dataOS.write(buf, 0, len);
System.out.println("dataOS.size() : " + dataOS.size());
}
_inputStream.close();
Run Code Online (Sandbox Code Playgroud)
以下是调试输出的最后几行:
len : 10240
Going to write buf into dataOS : 10240
dataOS.size() : 342804702
len : 10240
Going to write buf into dataOS : 10240
dataOS.size() : 342814942
len : 10240
Going …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 for 循环通过 DataOutputStream 发送多个 POST 请求,然后关闭它。目前,只有“ trades ”数组列表的第一个索引被发送到网站。任何其他索引都会被忽略,我假设它们没有被发送。我想知道我是否正确冲洗了溪流?谢谢你!!!
交易值示例:“101841599”、“101841801”
代码值示例:85e4c22
我的代码片段:
private ArrayList<String> trades = new ArrayList<String>();
private String code;
String url = "http://www.dota2lounge.com/ajax/bumpTrade.php";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
con.setRequestProperty("Cookie", cookie);
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
for(int i=0; i<trades.size(); i++){
wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes("trade=" + trades.get(i) + "&code=" + code);
wr.flush();
System.out.println("again");
}
wr.flush();
wr.close();
Run Code Online (Sandbox Code Playgroud) 我正在开发一个简单的客户端/服务器应用程序,它使用套接字进行所有通信.通信是基于数据包的,数据包的概念是使用一组类和ObjectInputStream/或ObjectOutputStream套接字流的包装器实现的.
想知道这种方法与基于全文本的协议(如IRC)或"非常二进制"的东西(我明确使用字节)相比是否有任何缺点.
让我们忽略这里的流量问题("qwerty"与"qwerty"+ 1KB的元数据)并且只考虑可靠性和可维护性.
你怎么看?
我正在使用对称密钥+ RSA开发FTP工具.我想通过dataoutputstream将我的密钥发送到服务器.我可以这样做吗?我试过跟着,
客户:
SecretKey secretKey = en_de_cryptor.returnSecretKey();
String encodedKey = Base64.encode(secretKey.getEncoded());
dout.writeUTF(secretKey.toString());
Run Code Online (Sandbox Code Playgroud)
服务器:
String secretKey = din.readUTF();
byte[] decodedKey = Base64.decode(secretKey);
Run Code Online (Sandbox Code Playgroud)
但我无法获得解码密钥.我该如何解决这个问题,并在服务器端获取密钥.
java dataoutputstream secret-key datainputstream javax.crypto
背景:我有一个任务,我将通过套接字传递信息到非常有限的程度.它可以是每个消息最多10个字节,我以为我只发送一个字节(因为一个字节足以在协议中发出256个不同的状态).现在我开始四处寻找有关这方面的信息,我遇到了很多问题.如果可以的话,请纠正我的假设错误并回答我的字面问题.
所以有原始数据类型字节(基本上是介于-128和127之间的数字,对吧?).如果我使用
byte b = 125;
System.out.println(b);
Run Code Online (Sandbox Code Playgroud)
...我得到正确的数字打印到控制台,如果我尝试分配超出限制的值,编译器会抱怨.
然后我们有类Byte,它显然是从字节数据类型(或API中的int)创建一个Byte对象:
Byte b = new Byte(20);
System.out.println(b);
Run Code Online (Sandbox Code Playgroud)
这也产生了预期的结果,并且20被打印到控制台,如果我尝试使用高于127的数字,编译器会抱怨.
1.数据类型字节和类字节有什么区别?是否主要是因为类提供了许多方法,如类Integer为类型int做的?
下一个片段会产生奇怪的结果(对我而言):
import java.io.*;
public class ByteTest {
public static void main(String[] args) {
DataInputStream in = new DataInputStream(System.in);
try {
byte d;
while((d = in.readByte()) != 0) {
System.out.println(d);
}
}
catch (IOException e) {
e.printStackTrace();
}
System.exit(0);
}
}
Run Code Online (Sandbox Code Playgroud)
2.输入被读取和输出将输入解释为十进制形式的ASCII字符(例如,1返回49),接着是另外两行,数字为13和10.这是为什么?
(如果我将d声明为字节或字节无关紧要,结果是相同的,我已经混合了从字节b获取值,依此类推,但这三行(或更多)始终是结果而我想要的只是输入回到我身边)
基本上我对此有点困惑,但最后,我想要的只是发送这些单个字节的合理方式,当我发送34时,我希望对方收到34,没有别的.
3.假设我没有使用类Byte,只想在流上发送一个类型字节.所有常规流和读取器似乎都只读取一个int(我假设它们会阻塞,直到它们至少有两个字节的输入,因为我只发送一个).我是否被迫使用DataInputStream并且 DataOutputStream我必须在对象字节中包装类型字节或者还有其他方法吗?
所有这一切让我怀疑我是否可以相信一个对象Byte真的只是一个字节的数据而已......我很困惑:(
提前致谢!
我有一个Java客户端,它将UTF-8字符串发送到C#TCP-Server,我正在使用DataOutputStream来发送字符串.代码如下所示:
public void sendUTF8String(String ar) {
if (socket.isConnected()) {
try {
dataOutputStream.write(ar.getBytes(Charset.forName("UTF-8")));
dataOutputStream.flush();
} catch (IOException e) {
handleException(e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,冲洗似乎不正常.如果我发送两个字符串彼此接近,服务器只接收一个包含两个字符串的消息.如果我在调用之间执行Thread.sleep(1000),整个过程都有效,这显然不是解决方案.我错过了什么?
我正在尝试在java中创建一个服务器,它将同时保持多达4个连接.我认为将相关信息保存在一个数组中可以达到我的目的,但是我遇到了一些麻烦.
这是我创建的课程:
import java.net.*;
import java.io.*;
public class tcpConnects{
private ObjectInputStream input;
private ObjectOutputStream output;
private int player;
public tcpConnects(int playerNumber, Socket connect) {
// TODO Auto-generated method stub
try{
System.out.println("create InputStream");
input= new ObjectInputStream(connect.getInputStream());
System.out.println("create OutputStream");
output= new ObjectOutputStream(connect.getOutputStream());
System.out.println("streams created");
//sendData("Welcome!");
player=playerNumber;
}catch (IOException ioException){
ioException.printStackTrace();
}
}
public ObjectInputStream getInput(){
return input;
}
public void setInput(ObjectInputStream in){
input=in;
}
public ObjectOutputStream getOutput(){
return output;
}
public void setOutput(ObjectOutputStream out){
output=out;
}
public int getPlayer(){
return player;
} …Run Code Online (Sandbox Code Playgroud) 我用 Java 编写了一个游戏,其中包含使用套接字和数据输入/输出流的客户端和服务器端。服务器端有时需要在“for”循环中向所有用户发送消息,但由于写入套接字可能会阻塞,因此我为每个向他发送消息的用户创建了一个线程(以及为每个监听的用户创建了另一个线程)传入消息)。发送线程是基于这个想法构建的:
private ArrayList<Object> messages = new ArrayList<Object>(),
newMessages = new ArrayList<Object>();
public void run() {
while (true) {
for (Object message: messages) {
try {
if (message instanceof Byte)
out.writeByte((Byte)message);
else if (message instanceof Boolean)
out.writeBoolean((Boolean)message);
else if (message instanceof String)
out.writeUTF((String)message);
else if (message instanceof Integer)
out.writeInt((Integer)message);
else if (message instanceof Long)
out.writeLong((Long)message);
} catch (IOException e) {}
}
synchronized (newMessages) {
messages.clear();
messages.addAll(newMessages);
newMessages.clear();
}
}
}
public void write(Object message) {
synchronized (newMessages) {
newMessages.add(message); …Run Code Online (Sandbox Code Playgroud) 我知道这可以使用包装器模式完成,但我很难理解,以下代码如何工作.
ByteArrayOutputStream bytearray = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(bytearray);
Run Code Online (Sandbox Code Playgroud)
将ByteArrayOutputStream引用传递给构造函数DataOutputStream意味着DataOutputStream转换为ByteArrayOutputStream,但如何?
在此之后,os.writeUTF("String");
如何DataOutputStream转换为ByteArrayOutputStream.
幕后发生了什么?有人可以解释一下细节.
我正在为现有客户端创建服务器.此客户端使用套接字和DataInputStream从服务器读取输入.它使用以下命令检查服务器消息的结束:
byte c = in.readByte();
if( c == 0) { //the end.
Run Code Online (Sandbox Code Playgroud)
在服务器上,我使用serversocket和DataOutputStream向客户端发送消息:
out.write(bytes[])
Run Code Online (Sandbox Code Playgroud)
如何发送0字节,以便客户端知道它是消息的结束?
我制作了自己的缓冲式打字机.但我不知道它是否确实如此?
当我退出时,我做了一个缓冲读取器(200个硬币),当我登录时,我得到(545453个硬币)或其他金额,我确信它是缓冲区的,请帮助!
public static int coins;
private static final String DIR = "./Data/";
public static boolean SavePlayer;
public static void saveAll() {
SavePlayer = true;
if (!SavePlayer) {
System.out.println("[WoG Error]: Their was an error saving players.");
return;
}
saveGame();
}
public static boolean saveGame() {
if (Player.playerName == null) {
return false;
}
try {
File file = new File(DIR + Player.playerName.toLowerCase() + ".dat");
if (!file.exists())
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
DataOutputStream o = new DataOutputStream(fileOutputStream);
o.writeUTF(Player.playerName);
o.writeInt(Player.coins);
//o.writeInt(Player.height); …Run Code Online (Sandbox Code Playgroud) 我在这段代码中遇到了一个奇怪的问题,当我使用第一个if条件执行代码时,它一切正常并且被视为已被删除.但是,当我评论if语句并使用另一个if条件(已经评论过的那个)时,它会给出NPE.getUserIP()和getPhoneNumber()都只是私有字符串的普通getter.并且两个值都是由普通的setter设置的.所以任何想法为什么会发生这种情况?谢谢.
public void sendBroadcast(final String broadcast) {
System.out.println("entered sendBroadcast");
String fullPeep=broadcast;
System.out.println("fullPeep: "+fullPeep);
String array[] = fullPeep.split("<!!>");
for(User tempUser: friends)
{
if(tempUser.getUserIP().equals(this.getUserIP()))
{
System.out.println("tempuser:" +tempUser.getPhoneNumber() + " user: "+array[1] );
//if(tempUser.getPhoneNumber().equals(array[1]))
//{
System.out.println("tempuser:" +tempUser.getPhoneNumber() + " user: "+array[1] );
System.out.println("if statemnt of broadcast method");
try {
DataOutputStream out2= new DataOutputStream(socket.getOutputStream());
out2=tempUser.getUserDataOutputStream();
out2.writeUTF(fullPeep+"\n");
out2.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
//}
}
}
Exception in thread "Thread-6" java.lang.NullPointerException
at User.sendBroadcast(User.java:180)
at Server$ServerThread.run(Server.java:394)
Run Code Online (Sandbox Code Playgroud)
编辑:我弄清楚导致异常的原因以及为什么tempUser.getPhoneNumber()在某些时候返回null.谢谢大家.
dataoutputstream ×13
java ×13
sockets ×4
append ×1
byte ×1
flush ×1
java-io ×1
javax.crypto ×1
post ×1
secret-key ×1
tcp ×1