Tag: io

在Java中,当使用DataOutputStream写入文件时,如何定义正在写入的数据的Endian?

我正在使用DataOutputStream写入文件,但我想更改数据的endian。 这就是我将字节数据写入文件的方式(默认情况下以Little endian输出) public void generateBinObjFile(String outputFile) try { // Create file DataOutputStream stream = new DataOutputStream( new FileOutputStream(outputFile)); stream.writeShort(this.quantize(this.xComponents.get(index), //<– Short is written in little Endian this.min_x, this.max_x) – 32768); } // catch statements here 有没有办法可以定义字节数据用Java编写的Endian?

从同一个FileInputStream中读取字符串和二进制文件

我有一个文件,在开始时包含一些纯文本,最后是二进制内容。 二进制内容的大小由我读过的一条纯文本行确定。 我使用BufferedReader来读取各行,但它没有公开任何方法来引用读取字节数组。 DataInputStream不会一直读到行尾,并且不推荐使用readLine方法。 使用底层的FileInputStream读取返回空字节数组。 关于如何解决这个问题的任何建议? private DOTDataInfo parseFile(InputStream stream) throws IOException{ DOTDataInfo info = new DOTDataInfo(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); int binSize = 0; String line; while((line = reader.readLine()) != null){ if(line.length() == 0) break; DOTProperty prop = parseProperty(line); info.getProperties().add(prop); if(prop.getName().equals(“ContentSize”)) binSize = Integer.parseInt(prop.getValue()); } byte[] content = new byte[binSize]; stream.read(content); //Its […]

PipedInputStream和PipedOutputStream的用例

Piped流的用例是什么? 为什么不将数据读入缓冲区然后将其写出来?

首次连接失败后sockets未连接

我不明白为什么以下程序在初始连接失败时不起作用(我在第一次连接失败后故意启动服务器套接字): Socket client = new Socket(); while (true) { try { client.connect(address); break; } catch (IOException e) { Thread.sleep(1000); } } 如果我移动client = new Socket(); 在while循环中,它可以工作,但我在文档中找不到任何说明如果初始连接失败,套接字被“破坏”并且必须被回收。

Java – 捕获System.err.println或捕获PrintStream

Java新手问题: 我需要捕获第三方组件正在写入printStream的文本。 PrintStream默认为System.err,但可以更改为另一个PrintStream。 浏览文档,我找不到一种简单的方法将PrintStream的内容定向到字符串编写器/缓冲区。 有人可以帮忙吗?

Java:从.txt文件LINE BY LINE中读取字节

请查看以下代码 import java.io.*; import java.util.ArrayList; import java.util.List; public class FileCopy2 { public static void main(String[]args) { try { //First in here, we are trying to get the bytes from the file File file = new File(“D:/burn/preview.mp3”); //The File you need to get byte bufferFile[] = new byte[(int)file.length()]; //Creating a byte array, which has the exact […]

将文件内容读入2D数组

我对编程很新,所以非常感谢外行。 我的任务是读取文件的内容,该文件将包含9个值(3×3数组),然后将内容放在相应的索引中。 例如, 2Darrays的内容是…… 1.00 -2.00 3.00 4.00 1.00 -1.00 1.00 2.00 1.00 读完内容后,需要打印。 然后程序将提示用户输入标量乘数,例如’4’。 然后程序应该使用新输出打印新数组。 我现在有这个, import java.io.*; import java.util.*; public class CS240Lab8a { /** * @param args the command line arguments */ static double [][] matrix = new double[3][3]; static Scanner input = new Scanner(System.in); public static void main(String[] args) throws IOException { […]

JTextArea txt; txt.getText()跳过“\ n”

我在TextArea中有一些文本,我想将其保存在文件中,我的代码在这里: private void SaveFile() { try { String content = txt.getText(); File file = new File(filename); if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close(); } catch (IOException e) { e.printStackTrace(); } } 但它没有“\ n”就可以保存; 在新文件中,一切都在一行; 我能预见那些“进入”吗? 先感谢您 问题是因为记事本,所以这里是解决方案: private void SaveFile() { try { String content […]

使用Java 7 API逐字节读取文件

我有一个2 gb文件,我想用Java读取(实际上是4个2gb文件)。 所以Java 7中有一个新function可以让我一次读取所有字节。 import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; public class reconstructor { public static void main(String[] args) throws IOException { Path p = Paths.get(“test.txt”); for (int i = 0; i < 1; i++) { byte[] b = Files.readAllBytes(p); Files.write(p, b, StandardOpenOption.APPEND); } } } 这是一个愚蠢的程序,它将读取一个预先输入单个字节的文件并连续读取该文件并将其读回的内容附加到同一文件中。 现在显然,RAM不够大,一次读取2gb文件,更不用说其中四个了,所以我想知道是否有任何快速方法,不使用外部库(除非这是唯一的方法)阅读四个逐字节文件,以便RAM不会过载(否则我最终会出现Java堆错误)。

Java InputStream的read(byte )方法

首先是一些背景。 它不需要回答实际的问题,但也许它有助于把事情放在眼里。 我在java(h)中编写了一个mp3库,它读取存储在.mp3文件中的ID3标签中的信息。 有关歌曲的信息,如歌曲名称,发行歌曲的CD,曲目编号等,都存储在.mp3文件开头的此ID3标签中。 我已经在12,579个mp3文件上测试了这个库,这些文件位于我的本地硬盘上,它运行完美。 没有一个IO错误。 当我执行相同的事情,其中​​mp3文件位于Web服务器上时,我收到IO错误。 好吧,实际上并不是一个错误。 实际上它是InputStream的read(byte [])方法行为的差异。 下面的例子将说明当我尝试从mp3文件中读取图像文件(.jpg,.gif,.png等)时出现的问题。 // read bytes from an .mp3 file on your local hard drive // reading from an input stream created this way works flawlessly InputStream inputStream = new FileInputStream(“song.mp3”); // read bytes from an .mp3 file given by a url // reading from an input […]