Tag: 文件

通过ObjectOutputStream发送文件,然后将其保存在Java中?

我有这个简单的服务器/客户端应用程序 我正在尝试让服务器通过OutputStream(FileOutputStream,OutputStream,ObjectOutputStream等)发送文件,并在将其保存到实际文件之前在客户端接收它。 问题是,我试过这样做,但它一直都失败了。 每当我创建文件并将从服务器收到的对象写入其中时,我都会得到一个损坏的图像(我只是将其保存为jpg,但这无关紧要)。 以下是最有可能出现故障的代码部分(您在此处看到的所有看似未声明的对象都已事先声明过): 服务器: ObjectOutputStream outToClient = new ObjectOutputStream( connSocket.getOutputStream()); File imgFile = new File(dir + children[0]); outToClient.writeObject(imgFile); outToClient.flush(); 客户: ObjectInputStream inFromServer = new ObjectInputStream( clientSocket.getInputStream()); ObjectOutputStream saveImage = new ObjectOutputStream( new FileOutputStream(“D:/ServerMapCopy/gday.jpg”)); saveImage.writeObject(inFromServer.readObject()); 所以,我的问题是我无法正确地通过流获取对象而不会丢失文件。

Java:创建临时文件并替换为原始文件

我需要一些创建文件的帮助 我在最后几个小时尝试使用RandomAccessFile并尝试实现下一个逻辑: 获取文件对象 创建一个具有相似名称的临时文件(如何确保临时文件将在给定原始文件的相同位置创建?) 写入此文件 用临时文件替换磁盘上的原始文件(应该是原始文件名)。 我寻找一个简单的代码谁更喜欢使用RandomAccessFile我只是不知道如何正确解决这几个步骤.. 编辑:好的,所以香港专业教育学院附上这部分代码,我的问题是,我无法理解应该是什么正确的步骤..文件没有被创建,我不知道该怎么做“切换” File tempFile = null; String[] fileArray = null; RandomAccessFile rafTemp = null; try { fileArray = FileTools.splitFileNameAndExtension(this.file); tempFile = File.createTempFile(fileArray[0], “.” + fileArray[1], this.file); // also tried in the 3rd parameter this.file.getParentFile() still not working. rafTemp = new RandomAccessFile(tempFile, “rw”); rafTemp.writeBytes(“temp file content”); tempFile.renameTo(this.file); } catch (IOException […]

将文件作为命令行参数传递并读取其行

这是我在互联网上找到的用于读取文件行的​​代码,我也使用eclipse,并在其参数字段中将文件名称作为SanShin.txt传递。 但它会打印: Error: textfile.txt (The system cannot find the file specified) 码: public class Zip { public static void main(String[] args){ try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream(“textfile.txt”); BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); String strLine; //Read File Line By Line while ((strLine = […]

Java的。 从FTP读取文件但不要整体下载

我需要从FTP读取CSV文件头。 由于这些文件非常庞大,我不需要下载它们。 有没有办法从FTP读取第一行CSV文件并中止连接?

Java – 性能问题; 变量名和文件名长度

好吧只是关于性能的一些问题,但我认为一些问题是合乎逻辑的: – 如果我使用短名称的变量将更好/更快地处理? 示例:“String s”将优于“String superphrase”。 – 关于文件名,如果文件名很短,访问速度会更快吗? 示例:(几乎相同)filename.txt或f.txt。 感谢所有人,我真的很想让我的软件变得更好:)

反序列化二进制类文件的内容时ClassNotFoundException

我对Java知之甚少。 我正在尝试读取包含int的文件以及名为“Automobile”的类的各种实例。 但是,当我反序列化它时,程序抛出一个ClassNotFoundException,我似乎无法理解为什么。 这是代码: try { FileInputStream fin = new FileInputStream(inputFile); ObjectInputStream input = new ObjectInputStream(fin); conto = input.readInt(); Automobile[] macchine = new Automobile[conto]; for(int i = 0; i < conto; i++) { macchine[i] = (Automobile)input.readObject(); } String targa; System.out.print("\nInserire le cifre di una targa per rintracciare l'automobile: "); targa = sc1.nextLine(); for(int i = […]

在Java中确定文件总字数的最佳方法是什么?

在Java中查找文本文件中单词总数的最佳方法是什么? 我认为Perl是最好的找到这样的东西。 如果这是真的那么从Java中调用Perl函数将是最好的? 在这样的条件下你会做什么? 还有更好的想法?

从spring MVC控制器返回xml文件

我已经尝试了很多从控制器函数返回文件。 这是我的function: @RequestMapping(value = “/files”, method = RequestMethod.GET) @ResponseBody public FileSystemResource getFile() { return new FileSystemResource(new File(“try.txt”)); } 我收到此错误消息: 无法写JSON: 没有为类java.io.FileDescriptor找到序列化程序,也没有发现创建BeanSerializer的属性 (为避免exception,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)) (通过参考链: org.springframework.core.io.FileSystemResource [\ “的OutputStream \”] – > java.io.FileOutputStream中[\ “FD \”]); 嵌套exception是com.fasterxml.jackson.databind.JsonMappingException:没有找到类java.io.FileDescriptor的序列化器,也没有发现创建BeanSerializer的属性 (为避免exception,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)) (通过参考链:org.springframework.core.io.FileSystemResource [\“outputStream \”] – > java.io.FileOutputStream [\“fd \”]) 有谁知道如何解决它? 而且,我应该如何从客户端发送(JavaScript,jQuery)?

在文件夹中创建文本文件

我想在我在这里创建的文件夹中创建一个文本文件。 File dir = new File(“crawl_html”); dir.mkdir(); String hash = MD5Util.md5Hex(url1.toString()); System.out.println(“hash:-” + hash); File file = new File(“”+dir+”\””+hash+”.txt”); 但是这段代码不会将文本文件创建到该文件夹​​中。相反,它会使文本文件位于该文件夹之外。

Java – 创建新文件,如何使用方法指定目录?

我知道如何通过这样做将file写入指定的目录: public void writefile(){ try{ Writer output = null; File file = new File(“C:\\results\\results.txt”); output = new BufferedWriter(new FileWriter(file)); for(int i=0; i<100; i++){ //CODE TO FETCH RESULTS AND WRITE FILE } output.close(); System.out.println("File has been written"); }catch(Exception e){ System.out.println("Could not create file"); } 但是,如果在方法中设置目录,我该如何继续指定目录? 例如,一个名为getCacheDirectory()方法。 假设所有必要的import等已经完成.. 谢谢 :)。