在Java中从传入字符串创建文件

在Java中从传入字符串创建文件,java,client,client-server,send,Java,Client,Client Server,Send,从我的客户机类中,我将发送字符串,一个用于文件名,一个用于内容 在服务器上,如何根据从客户端收到的字符串生成文件 客户端类: String theFile = "TextDoc.txt"; String theFileContent = "text inside TextDoc"; sendToServer.writeBytes(theFile +"\n"); sendToServer.writeBytes(theFileContent); 服务器类: BufferedReader

从我的客户机类中,我将发送字符串,一个用于文件名,一个用于内容

在服务器上,如何根据从客户端收到的字符串生成文件

客户端类:

 String theFile = "TextDoc.txt";
 String theFileContent = "text inside TextDoc";

 sendToServer.writeBytes(theFile +"\n");
 sendToServer.writeBytes(theFileContent);
服务器类:

 BufferedReader reFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

 String theFileFrCl = reFromClient.readLine();
 String theFileCtFrCl = reFromClient.readLine();
如何从客户端发送的内容生成文件?我不太确定如何将内容附加到TextDoc


Martyn。

在服务器端,我们收到了文件名和内容:

 BufferedReader reFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

 String theFileFrCl = reFromClient.readLine(); // file name.
 String theFileCtFrCl = reFromClient.readLine(); // contents.
在此之后,我们将编写文件:

FileOutputStream fos = new FileOutputStream(theFileFrCl,true); // opening file in append mode, if it doesn't exist it'll create one.
fos.write(theFileCtFrCl.getBytes()); // write contents to file TextDoc.txt
fos.close();
如果要将某些文件的内容返回给客户端,只需像以前一样从客户端请求文件名:

theFileFrCl = reFromClient.readLine(); // client will write and we'll read line here (file-name).
现在只需使用接收的文件名打开文件:

try{
  FileInputStream fis = new FileInputStream(theFileFrCl);
  byte data[] = new byte[fis.available()];
  fis.read(data);
  fis.close();
  toClient.writeBytes(data); // write to client.
}catch(FileNotFoundException fnf)
{
  // File doesn't exists with name supplied by client 
}

在服务器端,我们收到了文件名和内容:

 BufferedReader reFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

 String theFileFrCl = reFromClient.readLine(); // file name.
 String theFileCtFrCl = reFromClient.readLine(); // contents.
在此之后,我们将编写文件:

FileOutputStream fos = new FileOutputStream(theFileFrCl,true); // opening file in append mode, if it doesn't exist it'll create one.
fos.write(theFileCtFrCl.getBytes()); // write contents to file TextDoc.txt
fos.close();
如果要将某些文件的内容返回给客户端,只需像以前一样从客户端请求文件名:

theFileFrCl = reFromClient.readLine(); // client will write and we'll read line here (file-name).
现在只需使用接收的文件名打开文件:

try{
  FileInputStream fis = new FileInputStream(theFileFrCl);
  byte data[] = new byte[fis.available()];
  fis.read(data);
  fis.close();
  toClient.writeBytes(data); // write to client.
}catch(FileNotFoundException fnf)
{
  // File doesn't exists with name supplied by client 
}

[在Java文本文件中写入大数据的最快方法][1][1]:检查。[在Java文本文件中写入大数据的最快方法][1][1]:检查。该文件存储在哪里?如果客户机想要取回文件,我们是否将其发送回client.writeBytes(theFileFrCl);?此创建的文件将以简单的文字存储在当前工作目录中,与类所在的位置相同。若要首先返回文件内容,请要求返回文件名。然后读取文件并返回文件的内容。但如果您想在创建文件后立即返回内容,则选择“是”:oClient.writeBytes(theFileFrCl);好的。那我怎么还文件呢?我是否使用getName并在匹配项返回它?然后在当前目录中创建一个媒体目录,比如说“media”,并将其路径指定给FileOutputStream FileInputStream fis=new FileInputStream(“media\\”+FileFrcl);这里的媒体位于当前目录中,如果它位于D:drive中,我们会说FileInputStream(“D:\\media\\”+theFileFrCl)。它存储在哪里?如果客户机想要取回文件,我们是否将其发送回client.writeBytes(theFileFrCl);?此创建的文件将以简单的文字存储在当前工作目录中,与类所在的位置相同。若要首先返回文件内容,请要求返回文件名。然后读取文件并返回文件的内容。但如果您想在创建文件后立即返回内容,则选择“是”:oClient.writeBytes(theFileFrCl);好的。那我怎么还文件呢?我是否使用getName并在匹配项返回它?然后在当前目录中创建一个媒体目录,比如说“media”,并将其路径指定给FileOutputStream FileInputStream fis=new FileInputStream(“media\\”+FileFrcl);这里的媒体位于当前目录中,如果它位于D:drive中,我们将称为FileInputStream(“D:\\media\\”+theFileFrCl)。