我正在尝试通过 TCP/IP 套接字连接两个单独的应用程序。在这种情况下,两个应用程序都不作为另一个应用程序的“客户端”运行,将它们描述为两个需要相互通信的独立服务器会更容易。
为了接收数据,我正在使用该InputStream.Read()函数,一旦它收到一个值,-1它就应该停止处理。然而,现在的问题是,如果另一组数据出现,则InputStream已经位于流末尾 ( EOS),因此所有正在发送的新数据都将被丢弃或忽略。我发现解决此问题的唯一方法是,一旦到达流结束,我关闭套接字并重新打开它,我认为这可能可以通过其他方式更好地处理。
如何重置 InputStream 以便为可能出现的下一组数据做好准备?
这是代码:
package testpack;
import java.io.*;
public class InputStreamReadDemo {
private void readByte() throws IOException {
System.out.print("Enter the byte of data that you want to be read: ");
int a = System.in.read();
System.out.println("The first byte of data that you inputted corresponds to the binary value "+Integer.toBinaryString(a)+" and to the integer value "+a+".");
// tried writing System.in.close(); and System.in.reset();
}
private void readLine() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a line of text: ");
String res = br.readLine(); …Run Code Online (Sandbox Code Playgroud) 在java中,如何从输入流中读取固定长度并保存为文件?例如。我想从 inputStream 读取 5M,并保存为 downloadFile.txt 或其他内容。(BUFFERSIZE=1024)
FileOutputStream fos = new FileOutputStream(downloadFile);
byte buffer [] = new byte[BUFFERSIZE];
int temp = 0;
while ((temp = inputStream.read(buffer)) != -1)
{
fos.write(buffer, 0, temp);
}
Run Code Online (Sandbox Code Playgroud) java inputstream outputstream fileinputstream fileoutputstream
我有一个String[]数组,我需要将其转换为InputStream.
我见过Byte[]->InputStream和String-> InputStream,但没有见过这个。有小费吗?
我需要获取文件名字符串,并尝试打开该文件。如果找不到该文件,我将循环直到输入正确的字符串。
public static void main(String[] args){
// Get file string until valid input is entered.
System.out.println("Enter file name.\n Enter ';' to exit.");
String fileName = sc.nextLine();
boolean fileLoop = true;
InputStream inFile;
while (fileLoop){
try{
inFile = new FileInputStream(fileName);
fileLoop = false;
} catch (FileNotFoundException e) {
System.out.println("That file was not found.\n Please re enter file name.\n Enter ';' to exit.");
fileName = sc.nextLine();
if (fileName.equals(";")){
return;
}
}
}
// ****** This is where the error is. It says …Run Code Online (Sandbox Code Playgroud) 如何在设置超时值时获取进程的输出?
我目前正在使用 apache commons io utils 从进程的标准和错误输出创建一个字符串。
下面的代码按原样(带有注释)适用于终止的进程。但是,如果进程没有终止,主线程也不会终止!
如果我取消注释掉已注释的代码,而是注释掉 process.waitfor(),该方法将正确销毁非终止进程。但是,对于终止进程,无法正确获得输出。似乎一旦 waitfor() 完成,我就无法获得进程的输入和错误流?
最后,如果我尝试将注释部分移动到 process.waitfor() 当前所在的位置,删除 process.waitfor() 并取消注释注释部分,那么对于非终止进程,主线程也不会停止。这是因为 process.waitfor(15, ...) 永远不会到达。
private static Outputs runProcess(String command) throws Exception {
Process process = Runtime.getRuntime().exec(command);
// if (!process.waitFor(15, TimeUnit.SECONDS)) {
// System.out.println("Destroy");
// process.destroy();
// }
// Run and collect the results from the standard output and error output
String stdStr = IOUtils.toString(process.getInputStream());
String errStr = IOUtils.toString(process.getErrorStream());
process.waitFor();
return new Outputs(stdStr, errStr);
}
Run Code Online (Sandbox Code Playgroud) 我正在制作一个使用管道输入/输出流(周围有一个对象输入/输出流包装器)的java应用程序。我在发送数据时遇到了相当大的延迟,并认为这是因为我最初的数据大小所致。但是,以下演示显示了在不同线程上通信时管道输入/输出流中的本机延迟。
public class Main{
public static long millis = 0;
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
JButton button = new JButton("Press me");
frame.getContentPane().add(button);
frame.setSize(new Dimension(500, 500));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream(pis);
button.addActionListener(e -> {
try {
pos.write((int) (Math.random() * 1000));
//records time the packet was sent
millis = System.currentTimeMillis();
} catch (IOException e1) {
e1.printStackTrace();
}
});
while (true) {
System.out
.println("recieved: " + pis.read() + …Run Code Online (Sandbox Code Playgroud) 我在下面的代码中得到了“未发布的资源流”的强化发现。
Resource[] l_objResource = resourceLoader.getResources(configErrorCode);
Properties l_objProperty = null;
for (int i = 0; i < l_objResource.length; i++) {
l_objProperty = new Properties();
l_objProperty.load(l_objResource[i].getInputStream());
}
Run Code Online (Sandbox Code Playgroud)
该函数loadErrorCode()有时BaseErrorParser.java无法释放由getInputStream();
任何人都可以解释这一发现或帮助解决问题吗?
来自下面的评论,但上下文不清楚(JW):
ObjectInputStream l_objObjInputStream = null;
Map l_mapRet = null;
try {
l_objObjInputStream = new ObjectInputStream(new FileInputStream(p_objFilename));
Object l_objTemp = l_objObjInputStream.readObject();
l_mapRet = (Map) l_objTemp;
} finally {
if (l_objObjInputStream != null) {
l_objObjInputStream.close();
}
}
Run Code Online (Sandbox Code Playgroud) 我知道这个问题已经被问过好几次了,但我仍然无法通过这些解决方案来解决它。
我有一个 Maven 项目。一个 Config.java 文件位于consumer/src/main/java. 以下是内容:
import java.util.Properties;
public class Config {
Properties configFile;
public Config() {
configFile = new Properties();
try {
configFile.load(this.getClass().getClassLoader().
getResourceAsStream("property_table.config.txt"));
} catch(Exception e) {
e.printStackTrace();
}
}
public String getProperty(String key) {
String value = this.configFile.getProperty(key);
return value;
}
public static void main(String[] args) {
Config config = new Config();
System.out.println("URL: " + config.getProperty("URL"));
System.out.println("PASSWORD: " + config.getProperty("PASSWORD"));
}
}
Run Code Online (Sandbox Code Playgroud)
我不断得到nullpointer exception。我知道那是因为它找不到文件property_table.config.txt。
起初,我将property_table_config.txt文件放在与consumer/src/main/java/Config.java 文件相同的文件夹中()。并尝试使用 …
我想.txt使用 Java Stream在文件中查找字谜。这是我所拥有的:
try (InputStream is = new URL("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt").openConnection().getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
Stream<String> stream = reader.lines()) {
Run Code Online (Sandbox Code Playgroud)
以及字谜的方法:
public boolean isAnagram(String firstWord, String secondWord) {
char[] word1 = firstWord.replaceAll("[\\s]", "").toCharArray();
char[] word2 = secondWord.replaceAll("[\\s]", "").toCharArray();
Arrays.sort(word1);
Arrays.sort(word2);
return Arrays.equals(word1, word2);
}
Run Code Online (Sandbox Code Playgroud)
如何使用 Java 8 Stream 检查 unixdict.txt 中的单词是否是字谜?有没有办法将一个词与流中的所有词进行比较?
inputstream ×10
java ×10
outputstream ×2
anagram ×1
arrays ×1
iostream ×1
java-io ×1
java-stream ×1
process ×1
security ×1
sockets ×1
string ×1
system.in ×1