我在使用进程的重定向输入/输出时遇到了一些麻烦.最初,我有两个通过tcp/ip进行通信的应用程序.服务器告诉客户端打开cmd.exe,然后向客户端发出命令,客户端必须重定向到cmd.exe进程.然后客户端读取输出并将其发送回服务器.基本上我正在创建一种远程使用命令行的方法.
问题是它适用于第一个命令,然后没有任何事情.我能够在不使用tcp/ip的情况下重新创建问题.
Process p = new Process();
ProcessStartInfo psI = new ProcessStartInfo("cmd");
psI.UseShellExecute = false;
psI.RedirectStandardInput = true;
psI.RedirectStandardOutput = true;
psI.CreateNoWindow = true;
p.StartInfo = psI;
p.Start();
p.StandardInput.AutoFlush = true;
p.StandardInput.WriteLine("dir");
char[] buffer = new char[10000];
int read = 0;
// Wait for process to write to output stream
Thread.Sleep(500);
while (p.StandardOutput.Peek() > 0)
{
read += p.StandardOutput.Read(buffer, read, 10000);
}
Console.WriteLine(new string(buffer).Remove(read));
Console.WriteLine("--- Second Output ---");
p.StandardInput.WriteLine("dir");
buffer = new char[10000];
read = 0;
Thread.Sleep(500);
while (p.StandardOutput.Peek() > …Run Code Online (Sandbox Code Playgroud) 我正在绘制一个绘图部分,并编写了以下代码将图像保存到指定的相机文件夹.但是,我宁愿使用应用程序名称创建一个新文件夹,并将图像保存到该文件夹.怎么可能这样?
我还想稍后从该特定文件夹中检索图像文件.
谢谢!
String fileName = "ABC";
// create a ContentValues and configure new image's data
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, fileName);
values.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
values.put(Images.Media.MIME_TYPE, "image/jpg");
// get a Uri for the location to save the file
Uri uri = getContext().getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
try
{
OutputStream outStream = getContext().getContentResolver().openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush(); // empty the buffer
outStream.close(); // close the stream
Run Code Online (Sandbox Code Playgroud) 我想将我的XHTML文本转换为PDF.我将它转换为FileOutputStream但我找不到一种方法将它作为输入传递给ITextRenderer.这可能,怎么样?
代码 :
String finalXhtml=xhtmlparser(xmlText);
ByteArrayInputStream finalXhtmlStream = new ByteArrayInputStream(finalXhtml.getBytes());
String HTML_TO_PDF = "ConvertedFile.pdf";
OutputStream os = new FileOutputStream(HTML_TO_PDF);
ITextRenderer renderer = new ITextRenderer();
// renderer.loadDocument(finalXhtmlStream); i can pass a file here can i pass an input or output stream ?
renderer.layout();
renderer.createPDF(os) ;
os.close();
System.out.println("done.");
Run Code Online (Sandbox Code Playgroud)
注意:我可以将文件传递给ITextRenderer如下:
String File_To_Convert = "report.xhtml";
String url = new File(File_To_Convert).toURI().toURL().toString();
String HTML_TO_PDF = "ConvertedFile.pdf";
OutputStream os = new FileOutputStream(HTML_TO_PDF);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close(); …Run Code Online (Sandbox Code Playgroud) 我正在处理文件中的序列化和反序列化。此外,我很想使用FileOutputStreamwith ObjectOutputStream。问题是我有服务器/客户端聊天应用程序,每当客户端连接时,服务器必须检查连接的客户端之前是否已注册。因此,当客户端连接到服务器时,服务器会创建一个输出流,如
FileOutputStream fos = new FileOutputStream("src\\example\\mainList.txt");
Run Code Online (Sandbox Code Playgroud)
获取链表以检查此列表是否包含此对象。但是,FileOutputStream始终创建一个没有内容的新文件。我知道我是否将“真”值作为第二个参数传递给附加文件,但这个 .txt 文件应该只包含一个链接列表对象。因此,我不想在每个进程中附加一个列表。我该如何处理问题?我只是想要
我希望我已经清楚地告诉你我的问题,我会感谢你的每一个答案。所以还是谢谢
编辑:这是我想说的一个简单例子。
public class PersonTest2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
LinkedList<Person> list = new LinkedList<Person>();
Person per = new Person("John","Thompson",22);
Person per2 = new Person("Suzie","Cash",23);
Person per3 = new Person("Peter","Jackson",24);
Person per4 = new Person("Michael","Coch",25);
list.add(per);
list.add(per2);
list.add(per3);
list.add(per4);
FileOutputStream fos = new FileOutputStream("person.txt");
BufferedReader br = new BufferedReader(new FileReader("person.txt"));
if (br.readLine() == null ) {
System.out.println("No errors, …Run Code Online (Sandbox Code Playgroud) 我是Clojure的新手.我有多个线程试图写入输出流和如果我没有错误的套接字和它们的流不是线程安全意味着如果我同时写入它们可以混淆位.clojure的主要好处之一是对竞争条件进行内置并发处理.我如何在我的场景中使用它?
我试着研究原子,参考等等.我最初认为声明输出流是一个原子可以工作,但我不太确定,因为它似乎避免同时改变原子状态(使用交换!)但是我认为你可以从多个线程取消引用一个原子意味着多个线程将deref保存输出流的atom并同时写入它.
任何建议都将是最有帮助的.
提前致谢
(defn send-my-data [output data-bytes]
(try
(.write output)
(.flush output)
(catch Exception excp
(println (format "issue %s" (.printStackTrace excp))))
Run Code Online (Sandbox Code Playgroud)
现在我的所有线程都会在想要将数据写入输出流时调用此函数
我正在尝试使用HttpURLConnection执行post请求,但不知道如何正确执行.
我可以使用以下代码使用AndroidAsyncHttp客户端成功执行请求:
AsyncHttpClient httpClient = new AsyncHttpClient();
httpClient.addHeader("Content-type", "application/json");
httpClient.setUserAgent("GYUserAgentAndroid");
String jsonParamsString = "{\"key\":\"value\"}";
RequestParams requestParams = new RequestParams("request", jsonParamsString);
httpClient.post("<server url>", requestParams, jsonHttpResponseHandler);
Run Code Online (Sandbox Code Playgroud)
在桌面计算机上使用curl可以执行相同的请求:
curl -A "GYUserAgentAndroid" -d 'request={"key":"value"}' '<server url>'
Run Code Online (Sandbox Code Playgroud)
这两种方法都给了我服务器的预期响应.
现在我想使用HttpURLConnection做同样的请求.问题是我不知道如何正确地做到这一点.我尝试过这样的事情:
URL url = new URL("<server url>");
HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setDoInput(true);
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setRequestProperty("User-Agent", "GYUserAgentAndroid");
httpUrlConnection.setRequestProperty("Content-Type", "application/json");
httpUrlConnection.setUseCaches (false);
DataOutputStream outputStream = new DataOutputStream(httpUrlConnection.getOutputStream());
// what should I write here to output stream to post params to server ?
outputStream.flush();
outputStream.close();
// get …Run Code Online (Sandbox Code Playgroud) 请原谅模糊的标题(我不知道如何解决这个问题).无论如何,在我的代码中我明确地声明了一些变量,两个是有符号/无符号的int变量,其他是有符号/无符号的char类型变量.
我的代码:
#include <iostream>
int main(void)
{
unsigned int number = UINT_MAX;
signed int number2 = INT_MAX;
unsigned char U = UCHAR_MAX;
signed char S = CHAR_MAX;
std::cout << number << std::endl;
std::cout << "The size in bytes of this variable is: " << sizeof(number) << std::endl << std::endl;
std::cout << number2 << std::endl;
std::cout << "The size in bytes of this variable is: " <<sizeof(number2) << std::endl << std::endl;
std::cout << U << std::endl;
std::cout …Run Code Online (Sandbox Code Playgroud) 我想从远程存储中获取一个文件作为InputStream而不将其保存到本地文件系统.远程存储为Java API提供了一种方法,该方法接受OutputStream并将文件数据转储到其中.
void dump(OutputStream dest);
Run Code Online (Sandbox Code Playgroud)
我想出的简单方法是创建一个临时文件,将数据转储到其中并重新打开它作为InputStream.但是这种方法会创建一个临时文件.有没有代理文件可以轻松实现相同的结果?
我认为的调用operator<<会生成一个两参数的函数调用。那么,为什么不编译呢?
#include <iostream> // ostream
#include <iomanip> // setw, setfill
using std::ostream; using std::setw; using std::setfill;
struct Clock {
int h_, m_, s_;
Clock(int hours, int minutes, int seconds)
: h_{hours}, m_{minutes}, s_{seconds} {}
void setClock(int hours, int minutes, int seconds) {
h_ = hours; m_ = minutes; s_ = seconds;
}
friend ostream& operator<<(ostream&os, const Clock& c) {
auto w2 = [](ostream&os, int f) -> ostream& {
return os << setw(2) << setfill( '0' ) << f; …Run Code Online (Sandbox Code Playgroud) 我正在使用一个应用程序来获取gps位置,并在位图上将其绘制为圆圈,然后将其保存以继续进行操作,因此我需要重复阅读并保存文件。但是不幸的是,当我保存文件并读取文件时,经过多次迭代后文件已损坏...!
代码:
File output = new File(tmpDirectory, "map.jpg");
try {
OutputStream outputStream = new FileOutputStream(output);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception ex) {
Message("error!");
}
directory = tmpDirectory;//updating directory to load the manipulated image
readFile(directory + "map.jpg", false);//setting the image view new image
Run Code Online (Sandbox Code Playgroud)