Tag:

InputStreamReader与FileReader

除了初始化两者之外,我似乎无法确定InputStreamReader和FileReader之间的任何区别。 使用其中一个有什么好处吗? 大多数其他文章涵盖FileInputStream vs InputStreamReader ,但我与FileReader形成对比。 在我看来,他们都有相同的目的。

有没有办法确定输入流是否是readOnly?

我在想一些像 File file =new File(inputStream) // not possible though. file.canWrite File file =new File(inputStream) // not possible though. file.canWrite , 但我似乎无法找到一个解决方案将输入流转换为文件,而无需将其写入别处。 谢谢!

Java Audio Stream(mp3spi lib),UnsupportedAudioFileException

我看过有关流式MP3流的多个Stack Overflow问题(如Icecast)。 他们都说使用MP3SPI库,我就是这样。 MP3SPI用于支持audio/mpeg mime类型。 这就是我的Icecast流。 我正确地在我的类路径中有三个jar文件,但是在使用他们在示例中提供的相同代码时,我仍然得到UnsupportedAudioFileException : javax.sound.sampled.UnsupportedAudioFileException: could not get audio input str eam from input URL at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java: 1153) at DJUtils.testPlay(DJUtils.java:16) at DJ.play(DJ.java:13) at DJ.init(DJ.java:4) at Loader.main(Loader.java:69) 这是我的代码: public static void testPlay(){ try { AudioInputStream in= AudioSystem.getAudioInputStream(new URL(“http://localhost:8000/listen.m3u”)); AudioInputStream din = null; AudioFormat baseFormat = in.getFormat(); AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), […]

声纳违规:“方法可能无法关闭exception”

我有这个方法: private void unZipElementsTo(String inputZipFileName, String destPath) throws FileNotFoundException, IOException { OutputStream out = null; InputStream in = null; ZipFile zf = null; try { zf = new ZipFile(inputZipFileName); for (Enumeration em = zf.entries(); em.hasMoreElements();) { ZipEntry entry = em.nextElement(); String targetFile = destPath + FILE_SEPARATOR + entry.toString().replace(“/”, FILE_SEPARATOR); File temp = new File(targetFile); if […]

将String’temp’值的每个新值添加到ArrayList而不覆盖之前的’temp’

我的输入来自使用DataInputSteam的socket ,因为我可以将几个不同的String值全部分配给同一个clientDayOfWeek字符串,我无法弄清楚如何保存所有进入同一ArrayList的字符串值而不替换最后一个值。 如果可能的话,我也不想重复。 Socket socket = null; DataInputStream dataInputStream = null; dataInputStream = new DataInputStream( socket.getInputStream()); String clientDayOfWeek = dataInputStream.readUTF(); ArrayList ar = new ArrayList(); String temp = clientDayOfWeek; ar.add(temp); System.out.print(“Items in list: “+ ar);

使用一个套接字创建2个流

我正在尝试创建一个在互联网上运行的多人游戏。 问题是我正在尝试在流上获取输入,并且由于某种原因我无法使用一个套接字创建2个流,我将解释: Socket s=new Socket(“127.0.0.1”,5001); ObjectInputStream obj1=new ObjectInputStream(s.getInputStream()); ObjectInputStream obj2=new ObjectInputStream(s.getInputStream()); 有人可以解释一下为什么这段代码片段不起作用? 编辑:这是代码示例无法正常工作的另一个示例。 while (true) { try { objI = new ObjectInputStream(sock.getInputStream()); objO = new ObjectOutputStream(sock.getOutputStream()); }catch(Exception e) { e.printStackTrace(); } } 编辑2:非常感谢您的回答。 我试图刷新对象,但我不断收到此错误: java.io.StreamCorruptedException: invalid stream header: 33740003 at java.io.ObjectInputStream.readStreamHeader(Unknown Source) at java.io.ObjectInputStream.(Unknown Source) at Proccess.run(Proccess.java:22) at java.lang.Thread.run(Unknown Source)

使用InputStream通过TCP套接字接收多个图像

我每次从相机捕获图像时,我都会尝试从我的Android手机中自动发送多个图像到服务器(PC)。 问题是read()函数只在第一次阻塞。 因此,从技术上讲,只接收并完美显示一个图像。 但之后当is.read()返回-1 ,此函数不会阻塞,并且多个图像无法接收。 代码对于服务器来说很简单 while (true) { InputStream is = null; FileOutputStream fos = null; BufferedOutputStream bos = null; is = sock.getInputStream(); if (is != null) System.out.println(“is not null”); int bufferSize = sock.getReceiveBufferSize(); byte[] bytes = new byte[bufferSize]; while ((count = is.read(bytes)) > 0) { if (filewritecheck == true) { fos = new […]

从另一个进程的输出流中读取

我想在我的Java程序中读取c-Application的输出流。 iremoted (可在此处获取: http : //osxbook.com/software/iremoted/download/iremoted.c )是一个C-Application,如果按下Apple Remote遥控器上的按钮,它会输出单独的行,例如“0x19被按下”。 如果我启动iremoted程序,一切都很顺利,每次按下按钮,这些单独的行都显示在我的屏幕上。 现在我想在我的Java应用程序中读取c-application的输出流来处理Java项目中Apple Remote的输入。 不幸的是,我不知道为什么没有投入资产 ? 我尝试使用一个简单的HelloWorld.c程序,我的程序在这种情况下按预期响应(打印出HelloWorld)。 为什么它不适用于iremoted计划? public class RemoteListener { public void listen(String command) throws IOException { String line; Process process = null; try { process = Runtime.getRuntime().exec(command); } catch (Exception e) { System.err.println(“Could not execute program. Shut down now.”); System.exit(-1); } Reader inStreamReader = new […]

如何杀死BufferedInputStream .read()调用

我正在编写一个程序来从服务器下载非常大的文件(~2GB)。 我编写了程序以便能够恢复部分完成的下载, 为了模拟一个糟糕的互联网连接,我一直在下载中途时将我的以太网线从我的路由器中拉出来。 不幸的是,这导致我的程序挂起以下调用: while((bytesRead = in.read(data)) > 0) (其中bytesRead是int,in是从HttpURLConnection构建的BufferedInputStream,数据是字节数组)。 我试图通过在另一个线程上调用in.close()来“中断”调用,但是在恢复互联网连接之前它没有任何效果(此时会抛出exception)。 有什么方法可以阻止被切断的互联网连接冻结我的程序吗?

Java文件未使用新行字符写入流

我们正在从Web服务流式传输CSV文件。 看起来我们在流式传输时丢失了新的行字符 – 客户端将文件全部放在一行上。 知道我们做错了什么吗? 码: public static void writeFile(OutputStream out, File file) throws IOException { BufferedReader input = new BufferedReader(new FileReader(file)); //File input stream String line; while ((line = input.readLine()) != null) { //Read file out.write(line.getBytes()); //Write to output stream out.flush(); } input.close(); }