- N +

datainputstream,javascriptjava農村

如何eclipse編寫一個簡單實用的登陸界面

//服務器端代碼

importjava.awt.FlowLayout;

importjava.io.DataInputStream;

importjava.io.DataOutputStream;

importjava.io.IOException;

importjava.net.ServerSocket;

importjava.net.Socket;

importjava.util.ArrayList;

importjava.util.Collection;

importjava.util.Iterator;

importjavax.swing.JFrame;

importjavax.swing.JScrollPane;

importjavax.swing.JTextArea;

publicclassQLServerextendsJFrame{

publicJTextAreajtextarea=null;

publicvoidlanuchFrame(Stringstr){

this.setName(str);

init();

}

privatevoidinit(){

setLayout(newFlowLayout());

jtextarea=newJTextArea(20,17);

jtextarea.setLineWrap(true);

jtextarea.setEditable(false);

this.getContentPane().add(newJScrollPane(jtextarea));

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(200,400);

setLocationRelativeTo(null);

setResizable(false);

}

ServerSocketserver=null;

CollectioncClients=newArrayList<ClientConn>();//加個泛型

publicvoidstartServer()throwsIOException{

while(true){

Sockets=server.accept();

cClients.add(newClientConn(s));

jtextarea.append("newclientlogin"+s.getInetAddress()+":"+s.getPort()+"\n");

}

}

publicQLServer(intport,Stringstr)throwsIOException{

server=newServerSocket(port);

lanuchFrame(str);

}

classClientConnimplementsRunnable

{

Sockets=null;

publicClientConn(Sockets)

{

this.s=s;

(newThread(this)).start();

}

publicvoidsend(Stringstr)throwsIOException

{

DataOutputStreamdos=newDataOutputStream(s.getOutputStream());

dos.writeUTF(str);

}

publicvoiddispose()//客戶端下線

{

try{

if(s!=null)s.close();

cClients.remove(this);

jtextarea.append("Aclientout!\n");

jtextarea.append("clientcount:"+cClients.size()+"\n\n");

}

catch(Exceptione)

{

e.printStackTrace();

}

}

publicvoidrun()

{

try{

DataInputStreamdis=newDataInputStream(s.getInputStream());

Stringstr=dis.readUTF();

while(str!=null&&str.length()!=0)

{

System.out.println(str);

for(Iteratorit=cClients.iterator();it.hasNext();)

{

ClientConncc=(ClientConn)it.next();

if(this!=cc)

{

cc.send(str+""+s.getInetAddress().getHostName());

}

}

str=dis.readUTF();//少了這句話會無限輸出

//send(str);

}

this.dispose();

}

catch(Exceptione)

{

this.dispose();

}

}

}

publicstaticvoidmain(String[]args){

try{

QLServerqlserver=newQLServer(8888,"QLServer");

qlserver.startServer();

}catch(IOExceptione){

e.printStackTrace();

}

}

}importjava.awt.FlowLayout;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.io.BufferedReader;

importjava.io.DataInputStream;

importjava.io.DataOutputStream;

importjava.io.IOException;

importjava.io.InputStreamReader;

importjava.net.InetAddress;

importjava.net.Socket;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JScrollPane;

importjavax.swing.JTextArea;

//客戶端代碼

publicclassQLClientextendsJFrameimplementsActionListener{

publicJTextAreajtextarea1=null;

publicJTextAreajtextarea2=null;

publicJButtonbutton=null;

Sockets=null;

publicvoidlaunchFrame(Stringstr){

this.setName(str);

init();

}

publicQLClient(Stringstr)throwsIOException{

launchFrame(str);

s=newSocket("127.0.0.1",8888);//哪臺電腦做服務器,IP地址改成那臺機子的IP

(newThread(newServeConn())).start();

}

privatevoidinit(){

setLayout(newFlowLayout());

jtextarea1=newJTextArea(17,16);

jtextarea2=newJTextArea(4,16);

jtextarea1.setLineWrap(true);

jtextarea1.setEditable(false);

jtextarea2.setLineWrap(true);

button=newJButton("發送");

button.addActionListener(this);//綁定button事件

this.getContentPane().add(newJScrollPane(jtextarea1));

this.getContentPane().add(newJScrollPane(jtextarea2));

add(button);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(200,470);

setLocationRelativeTo(null);

setResizable(false);

}

publicvoidsend(Stringstr)throwsIOException{

DataOutputStreamdos=newDataOutputStream(s.getOutputStream());

dos.writeUTF(str);

}

publicvoidactionPerformed(ActionEvente){

if(e.getSource()==button){

StringsendStr=jtextarea2.getText();

if(sendStr.trim().length()==0){

return;

}

try{

this.send(sendStr);

jtextarea2.setText("");

InetAddressa;

a=InetAddress.getLocalHost();

Stringhostname=a.getHostName();

jtextarea1.append(sendStr+"("+hostname+")"+"\n");

}catch(IOExceptione1){

//TODOAuto-generatedcatchblock

e1.printStackTrace();

}

}

}

classServeConnimplementsRunnable{

publicvoidrun(){

if(s==null)return;

try{

DataInputStreamdis=newDataInputStream(s.getInputStream());

Stringstr=dis.readUTF();

while(str!=null&&str.length()!=0)

{

//System.out.println(str);

QLClient.this.jtextarea1.append(str+"\n");//內部類用外類中的變量或方法加外類名

str=dis.readUTF();

}

}

catch(Exceptione)

{

e.printStackTrace();

}

}

}

//main主函數入口

publicstaticvoidmain(String[]args)throwsIOException{

BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));

QLClientqlclient=newQLClient("QLClient");

Stringstr=br.readLine();

while(str!=null&&str.length()!=0){

qlclient.send(str);

str=br.readLine();//防止死循環

}

qlclient.s.close();

}

}

fileinputstream文件不存在

應該輸入文件的路徑不正確,所以找不到。

datainputstream可以讀取什么文件

DataInputStream是Java中的輸入流,它可以從字節流中讀取基本數據類型(如int,double等)和字符串等。因此,DataInputStream可以讀取任何二進制文件。

但是,需要注意的是,使用DataInputStream讀取文件時,需要先使用相應的輸出流(比如DataOutputStream)將數據寫入文件,并在讀取時按照相同的順序讀取數據。如果讀取順序不正確,可能會導致數據解析錯誤或讀取失敗。

另外,對于文本文件,建議使用其他類型的輸入流(如BufferedReader),因為它們提供了更高級的文本文件讀取功能,比使用DataInputStream更方便和安全。

如何使用Hadoop的DistributedCache

操作步驟:1.將數據的分發到每個節點上:DistributedCache.addCacheFile(newURI("hdfs://cloud01:9000/user/hadoop/mrinput/ST.txt"),conf);注意,此操作一定要在創建Job,將conf傳遞給Job之前進行,否則數據文件的路徑不會被Mapper中取到。2.在每個Mapper中獲取文件URI,再進行相關操作:URI[]uris=DistributedCache.getCacheFiles(context.getConfiguration());比如讀取該文件:FileSystemfs=FileSystem.get(URI.create("hdfs://cloud01:9000"),context.getConfiguration());FSDataInputStreamin=null;in=fs.open(newPath(uris[0].getPath()));BufferedReaderbr=newBufferedReader(newInputStreamReader(in));hadoop中的DistributedCache2WordCount.javaHadoop的分布式緩存機制使得一個job的所有map或reduce可以訪問同一份文件。在任務提交后,hadoop將由-files和-archive選項指定的文件復制到HDFS上(JobTracker的文件系統)。在任務運行前,TaskTracker從JobTracker文件系統復制文件到本地磁盤作為緩存,這樣任務就可以訪問這些文件。對于job來說,它并不關心文件是從哪兒來的。在使用DistributedCache時,對于本地化文件的訪問,通常使用SymbolicLink來訪問,這樣更方便。通過URIhdfs://namenode/test/input/file1#myfile指定的文件在當前工作目錄中被符號鏈接為myfile。這樣job里面可直接通過myfile來訪問文件,而不用關心該文件在本地的具體路徑。

ByteArrayInputStream能轉換成FileInputStream么

1、將File、FileInputStream轉換為byte數組:Filefile=newFile("file.txt");InputStreaminput=newFileInputStream(file);byte[]byt=newbyte[input.available()];input.read(byt);

2、將byte數組轉換為InputStream:byte[]byt=newbyte[1024];InputStreaminput=newByteArrayInputStream(byt);

3、將byte數組轉換為File:Filefile=newFile('');OutputStreamoutput=newFileOutputStream(file);BufferedOutputStreambufferedOutput=newBufferedOutputStream(output);bufferedOutput.write(byt);

FileInputStream和BufferedInputStream read()的區別

FileInputStream是字節流,BufferedInputStream是字節緩沖流,使用BufferedInputStream讀資源比FileInputStream讀取資源的效率高(BufferedInputStream的read方法會讀取盡可能多的字節),且FileInputStream對象的read方法會出現阻塞;

在FileInputStream里有一個說明是說此方法將阻塞,意思就是說在你讀一個文件輸入流的時候,當讀到某個位置的時候,如果做一些其他處理(比如說接受一部分字節做一些處理等等)這個時候輸入流在什么位置就是什么位置,不會繼續往下讀,而BufferedInputStream雖然也有一個read方法,但是從名字就可以看出,它帶有一個緩沖區,它是一個非阻塞的方法,在你讀到某個位置的時候,做一些處理的時候,輸入流可能還會繼續讀入字節,這樣就達到了緩沖的效果。

對于性能要求不高的時候,用哪個都無所謂,但是如果有性能要求,建議還是用BufferedInputStream。

返回列表
上一篇:
下一篇: