请大家帮助解释一个C++函数必须声明吗声明

BufferedWriter.write()与BufferedReader.readLine() (附带Java中的Runtime exec)
时间: 21:16:32
&&&& 阅读:75
&&&& 评论:
&&&& 收藏:0
标签:昨天在实现一个Java程序启动执行C++程序中,遇到了一些问题,先准备把它记录下来(利人利己)
准备实现的测试功能是这样的:在一个java程序中启动一个C++可执行程序,然后java程序和C++程序相互通信(java端发送消息给C++程序处理,C++处理完成后将返回一个结果消息给java程序。不断往返循环直至输入exit指令结束)。
首先,在一个java程序中启动一个C++可执行程序 使用的是java的Runtime.getRuntime().exec(),&/javase/6/docs/api/java/lang/Process.html&
【另外,还有一种java的JNI (Java Native Interface)框架可以实现java调用其它编程语言的方法】
使用exec()方式,C++程序的所有标准 io(即 stdin、stdout和
stderr)操作都将通过三个流(getOutputStream()、getInputStream()和
getErrorStream())&重定向到父进程。
因此,使用上述重定向特性就可以通过标准输入输出实现Java与C++程序间的简单通信,在处理通信I/O时,使用了BufferedReader和BufferedWriter作为通信输入输出的缓冲,方便对消息处理。
关于readLine(),此处有篇博文&http://blog.csdn.net/swingline/article/details/5357581
1. 由于C++端是按行读取消息的,所以在Java端发送消息时要注意每个消息的结尾都必须加上换行符。这就是为何我在writeToCpp.write(wstr+&\n&);&中要+&\n&的原因。(就是因为这个换行符没加导致我调bug费了好长时间,之前一直找不出什么原因导致java端在console中输入消息发送后java程序就跑飞了。。后来发现在eclipse的console中输入字符串回车后该字符串末尾是不带换行符的。。。)
2. 下面的java代码中开辟了一个线程来处理从C++端传送来的消息打印。非常好用!
3. 【引用自】
误以为readLine()是读取到没有数据时就返回null(因为其它read方法当读到没有数据时返回-1),而实际上readLine()是一个阻塞函数,当没有数据读取时,就一直会阻塞在那,而不是返回null;因为readLine()阻塞后,System.out.println(message)这句根本就不会执行到,所以在接收端就不会有东西输出。要想执行到System.out.println(message),一个办法是发送完数据后就关掉流,这样readLine()结束阻塞状态,而能够得到正确的结果,但显然不能传一行就关一次数据流;另外一个办法是把System.out.println(message)放到while循环体内就可以。
另外有个问题没解决:
针对Java启动C++程序场景,如何配置使用eclipse和visual studio (2013)使得我能够从eclipse中debug Java程序跟踪到C++程序后能在Visual studio中继续能debug呢?
我查了下,好像可以通过Visual studio的attach to process功能能够实现联调(在VS中依次打开Debug -& Attach to process. 然后找到启动的java进程选中确定【我选的是javaw.exe】),但是我尝试了好久没有成功。(一开始提示找不到PDB文件,我就去vs的TOOLS -& Options -& Debugging -& Symbols勾选了“Microsoft Symbol Servers” ,部分PDB文件就可以加载了,但还有部分PDB文件加载不上。。。打的断点也是显示黄色圆圈带感叹号
【提示信息见下文】)
还请有经验的大侠能帮忙提供下eclipse与VS联调的解决方案,谢谢!!
下面就是实现了双边通信的java和C++源代码
Java端代码片段:
import java.io.BufferedR
import java.io.BufferedW
import java.io.IOE
import java.io.InputStreamR
import java.io.OutputStreamW
import java.util.S
public class JavaCallCpp {
static BufferedWriter writeToC
static BufferedReader readFromC
public static void main(String[] args) throws IOException {
Process execute = Runtime.getRuntime().exec(&D:/WorkSpace/vs/stringstream/Debug/stringstream&);
//int exitVal = execute.waitFor();
writeToCpp = new BufferedWriter(new OutputStreamWriter(execute.getOutputStream()));
readFromCpp = new BufferedReader(new InputStreamReader(execute.getInputStream()));
MyThread myThread=new MyThread(readFromCpp);
myThread.start();
/*Write to C++*/
writeToCpp.write(&have entered the scentence\n&);
writeToCpp.write(&aaaaa\n&);
writeToCpp.flush();
//writeToCpp.write(&quit\n&);
//writeToCpp.flush();
/*Read from C++*/
writeToCpp.write(&abcd\n&);
writeToCpp.flush();
Scanner writer = new Scanner(System.in);
while(true){
String wstr =
while(true){
wstr=writer.next();
if(wstr.equals(&exit&)){
writer.close();
System.out.println(wstr);
writeToCpp.write(wstr+&\n&);
writeToCpp.flush();
writeToCpp.flush();
System.out.println(&out of while.&);
/*while((readLine = readFromCpp.readLine()) != null){
System.out.println(readLine);
//writerString.close();
execute.destroyForcibly();//强制终止
execute.getInputStream().close();
readFromCpp.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}class MyThread extends Thread{
private BufferedR
public MyThread(BufferedReader bfr){
public void run() {
// TODO Auto-generated method stub
super.run();
String readLine =
Boolean _b=
while(_b){
readLine=bfr.readLine();
if(readLine==null)
System.out.println(readLine);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
C++端程序片段:
void fun1(){
string line,
cout && &enter a scentence, and exit with typing \&quit\&&&&
while (getline(cin, line)&& line!=&quit&)
stringstream stream(line);
cout && stream.str() &&
while (stream && word){ cout && word && }
while (true){
cout && &please input a command, and exit with typing \&exit\&: &&&
getline(std::cin, cmd);
if (0==strcmp(cmd.c_str(),&exit&)){ //strcmp返回str1-str2的值
cout && &cmd is exit, exit 0 \n&;
else if (strcmp(cmd.c_str(), &continue&) !=0){
cout && &cmd is not \&continue\& to break\n&;
cout && &cmd is continue, break while(true) \n&;
cout && &OK, pass over while(true) \n&;
/*以下是针对debug问题的(可忽略)*/
eclipse与visual studio联调debug的提示信息:
'javaw.exe' (Win32): Loaded 'C:\Program Files\Java\jdk1.8.0_65\bin\javaw.exe'. Cannot find or open the PDB file.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\ntdll.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\kernel32.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\KernelBase.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\advapi32.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\msvcrt.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\sechost.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\rpcrt4.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\user32.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\gdi32.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\WinSxS\amd64_mon-controls_ccf1df_6.0.10586.0_none_8c15ae\comctl32.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\combase.dll'. Cannot find or open the PDB file.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\bcryptprimitives.dll'. Cannot find or open the PDB file.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\imm32.dll'. Cannot find or open the PDB file.
'javaw.exe' (Win32): Loaded 'C:\Program Files\Java\jdk1.8.0_65\jre\bin\msvcr100.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Program Files\Java\jdk1.8.0_65\jre\bin\server\jvm.dll'. Cannot find or open the PDB file.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\psapi.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\wsock32.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\ws2_32.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\version.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\winmm.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\winmmbase.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\cfgmgr32.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Program Files\Java\jdk1.8.0_65\jre\bin\verify.dll'. Cannot find or open the PDB file.
'javaw.exe' (Win32): Loaded 'C:\Program Files\Java\jdk1.8.0_65\jre\bin\java.dll'. Cannot find or open the PDB file.
'javaw.exe' (Win32): Loaded 'C:\Program Files\Java\jdk1.8.0_65\jre\bin\jdwp.dll'. Cannot find or open the PDB file.
'javaw.exe' (Win32): Loaded 'C:\Program Files\Java\jdk1.8.0_65\jre\bin\npt.dll'. Cannot find or open the PDB file.
'javaw.exe' (Win32): Loaded 'C:\Program Files\Java\jdk1.8.0_65\jre\bin\zip.dll'. Cannot find or open the PDB file.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\shell32.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\windows.storage.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\shlwapi.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\kernel.appcore.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\SHCore.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\powrprof.dll'. Cannot find or open the PDB file.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\profapi.dll'. Cannot find or open the PDB file.
'javaw.exe' (Win32): Loaded 'C:\Program Files\Java\jdk1.8.0_65\jre\bin\dt_socket.dll'. Cannot find or open the PDB file.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\NapiNSP.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\pnrpnsp.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\nlaapi.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\mswsock.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\dnsapi.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\nsi.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\winrnr.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\wshbth.dll'. Symbols loaded.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\FWPUCLNT.DLL'. Cannot find or open the PDB file.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\bcrypt.dll'. Cannot find or open the PDB file.
'javaw.exe' (Win32): Loaded 'C:\Windows\System32\rasadhlp.dll'. Cannot find or open the PDB file.
&&国之画&&&& &&&&chrome插件
版权所有 京ICP备号-2
迷上了代码!Java.io.Console.readLine() Method Example
Java.io.Console.readLine() Method
Advertisements
Description
The java.io.Console.reader() method reads a single line of text from the console.
Declaration
Following is the declaration for java.io.Console.readLine() method &
public String readLine()
Parameters
Return Value
This method returns the string containing the line read from the console, not including any line termination character, or null if an end of the stream has been reached.
IOError & If an I/O error occurs.
The following example shows the usage of java.io.Console.readLine() method.
package com.
import java.io.C
public class ConsoleDemo {
public static void main(String[] args) {
Console cnsl =
String name =
// creates a console object
cnsl = System.console();
// if console is not null
if (cnsl != null) {
// read line from the user input
name = cnsl.readLine("Name: ");
System.out.println("Name entered : " + name);
} catch(Exception ex) {
// if any error occurs
ex.printStackTrace();
Let us compile and run the above program, this will produce the following result &
Name: Master Programmer
Name entered : Master Programmer
java_io_console.htm
& Copyright 2017. All Rights Reserved.学习Console.Write() Console.WriteLine() Console.Read() Console.ReadLine()方法的使用 -
- ITeye博客
博客分类:
从今天开始认认真真地学习C#,以前都是半玩半学.从今天开始每天学习 有计划地学习
今天学习Console.Write() Console.WriteLine() Console.Read() Console.ReadLine()方法的使用 看了书再照着书写了程序:
//file_name:read.cs//date:22:38 日//for:熟悉Console.read()&Console.ReadLine()的方法using Sclass read_info{ public static void Main(string[] args) {
//Console.ReadLine()的方法
Console.Write("请输入你的姓名:");//Console.Write()方法是不换行输出信息
string s = Console.ReadLine();
//此方法是读取输入的名字并把它存入到字符串s中;
Console.WriteLine("Hi,{0}.Welcome",s);//Console.WriteLine()是先输出信息再换行
//Console.read()方法
Console.Write("请输入你的生日:");
int i = Console.Read();
Console.Write("您的生日是:{0}!",i); }}
//Console.Read() Console.ReadLine()方法都是从键盘读入信息,唯一不同的就是Console.Read() 方法用于获得用户输入的任何值(可以是任何的字母数字值)的ASCII值.Console.ReadLine()呢?用于将获得的数据保存在字符串变量之中.
浏览: 484930 次
来自: 北京
还可以了,po主的第一个方法可行,第二个方法没有试
Soongtracy 写道方法二完全不行啊亲,你自己有木有试过 ...
您好,有个问题想请教一下:&birt生成Excel缺 ...
楼主,你找到原因了吗?这是为啥?貌似是JVM出bug了java tcp简单例子 - 不积跬步 无以至千里 不积小流 无以成江海 - ITeye博客
博客分类:
package com.justsee.
import java.io.BufferedR
import java.io.IOE
import java.io.InputS
import java.io.InputStreamR
import java.io.OutputS
import java.net.ServerS
import java.net.S
public class TcpServer {
//先启动服务器端程序
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=new ServerSocket(8080);
Socket socket=serverSocket.accept();//阻塞等待消息
InputStream inputStream=socket.getInputStream();
OutputStream outputStream=socket.getOutputStream();
outputStream.write("欢迎光临!".getBytes(), 0,"欢迎光临!".getBytes().length );
outputStream.write("welcome!".getBytes() );
/* byte[] buf=new byte[1024];
int len=inputStream.read(buf);
System.out.println(new String(buf, 0, len));
inputStream.close();*/
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
System.out.println(bufferedReader.readLine());
bufferedReader.close();
outputStream.close();
socket.close();
serverSocket.close();
客户端(还可以开多个telnet来模拟)
package com.justsee.
import java.io.BufferedR
import java.io.DataOutputS
import java.io.InputS
import java.io.InputStreamR
import java.io.OutputS
import java.net.InetA
import java.net.S
//后启动客户端程序
public class TcpClient {
//启动main时传进来两个参数:ip和端口号
public static void main(String [] args)
//Socket s=new Socket(InetAddress.getByName("192.168.0.213"),8001);
if(args.length & 2)
System.out.println("Usage:java TcpClient ServerIP ServerPort");
//建立Socket
Socket s=new Socket(InetAddress.getByName(args[0]),Integer.parseInt(args[1]));
InputStream ips=s.getInputStream();
OutputStream ops=s.getOutputStream();
BufferedReader brKey = new BufferedReader(new InputStreamReader(System.in));//键盘输入
DataOutputStream dos = new DataOutputStream(ops);
BufferedReader brNet = new BufferedReader(new InputStreamReader(ips));
while(true)
String strWord = brKey.readLine();
dos.writeBytes(strWord + System.getProperty("line.separator"));
if(strWord.equalsIgnoreCase("quit"))
System.out.println(brNet.readLine());
dos.close();
brNet.close();
brKey.close();
s.close();
}catch(Exception e){e.printStackTrace();}
/*发送和接受都是以Socket为信息载体*/
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.justsee.
import java.io.BufferedR
import java.io.DataOutputS
import java.io.IOE
import java.io.InputS
import java.io.InputStreamR
import java.io.OutputS
import java.net.ServerS
import java.net.S
public class Servicer implements Runnable {
public Servicer(Socket socket) {
this.socket =
public void run() {
InputStream inputStream=socket.getInputStream();
OutputStream outputStream=socket.getOutputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
DataOutputStream dataOutputStream=new DataOutputStream(outputStream);
while (true) {
String strWord=bufferedReader.readLine();
System.out.println(strWord+":"+strWord.length());
if ("quit".equalsIgnoreCase(strWord)) {
String strEcho=(new StringBuilder(strWord).reverse()).toString();
//dataOutputStream.writeBytes(strWord+"----&"+strEcho+"\r\n");
//System.getProperty("line.separator")为跨平台换行符。Window为\r\n,unix为\n
dataOutputStream.writeBytes(strWord+"----&"+strEcho+System.getProperty("line.separator"));
bufferedReader.close();
bufferedReader.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
public static void main(String [] args)
ServerSocket ss=new ServerSocket(8080);
while(true)
Socket s=ss.accept();
new Thread(new Servicer(s)).start();
//ss.close();
}catch(Exception e){e.printStackTrace();}
浏览 16168
浏览: 2474664 次
来自: China
获取原型对象的三种方法&script&functi ...
属性的重写与删除与原型链无关&script&fun ...
为什么没生效啊!!!弄了几次了!!!
史上最详细的iOS之事件的传递和响应机制-原理篇http:// ...
事件的传递和响应的区别:事件的传递是从上到下(父控件到子控件) ...}

我要回帖

更多关于 函数必须声明吗 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信