io

ObjectOutputStream methods: writeBytes(String str) vs writeUTF(String s);

匆匆过客 提交于 2020-05-11 07:44:06
问题 What's the main difference between the two? Still both of them are for writing Strings. public void writeUTF(String str) throws IOException Primitive data write of this String in modified UTF-8 format. vs public void writeBytes(String str) throws IOException Writes a String as a sequence of bytes. When should I use one rather than the other? 回答1: It's in the documentation... from DataOutput.writeBytes(String): Writes a string to the output stream. For every character in the string s, taken in

Preferred way to use Java ZipOutputStream and BufferedOutputStream

被刻印的时光 ゝ 提交于 2020-05-09 18:37:08
问题 In Java does it matter whether I instantiate a ZipOutputStream first, or the BufferedOutputStream first? Example: FileOutputStream dest = new FileOutputStream(file); ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(dest)); // use zip output stream to write to Or: FileOutputStream dest = new FileOutputStream(file); BufferedOutputStream out = new BufferedOutputStream(new ZipOutputStream(dest)); // use buffered stream to write to In my non-scientific timings I can't seem to

c sscanf with character delimiter

痴心易碎 提交于 2020-05-08 15:41:47
问题 I have an input string of the form char *s = "one.two three" and I want to separate it into 3 string variables. I'm doing sscanf(s, "%s.%s %s", one, two, three); but it's reading in "one.two" as the string for variable one. How do I handle the "." and the whitespace with sscanf? 回答1: The %s specifier only stops for a space. Try something like: sscanf(s, "%[^.].%s %s", one, two, three); Interestingly, it's likely impossible to produce an acceptable input for "%s.%s %s" since %s only stops for

Treat a string as a file in python

陌路散爱 提交于 2020-04-29 12:49:27
问题 In the interest of not rewriting an open source library, I want to treat a string of text as a file in python 3. Suppose I have the file contents as a string: not_a_file = 'there is a lot of blah blah in this so-called file' I want to treat this variable, i.e. the contents of a file, as a path-like object that way I can use it in python's open() function. Here is a simple example which shows my dilemma: not_a_file = 'there is a lot of blah blah in this so-called file' file_ptr = open(not_a

Reading a text file in the Producer paradigm

社会主义新天地 提交于 2020-04-18 05:48:40
问题 There is a task to read a text file in a producer paradigm. The producer interface is defined as the following: public interface Producer<ITEM> { /** * Produces the next item. * * @return produced item */ ITEM next(); /** * Tells if there are more items available. * * @return true if there are more items, false otherwise */ boolean hasNext(); } Current code to read the text file is: public static void readTextFile(File file, Charset charset, Consumer<String> consumer) { try (InputStreamReader

Failed to delete a file in Windows using Java

我怕爱的太早我们不能终老 提交于 2020-04-12 16:51:36
问题 I have been trying to delete a file in windows operating system using the Java IO file.delete() API. However it fails and returns false. The same code works like a charm in Ubuntu. I have verified that the permissions of the file allows the program to delete it. Also all the input and output stream for the file has been opened as try with resources. try (InputStream in = new FileInputStream(localFile); OutputStream out = new FileOutputStream(destinationFileName)) Using a debugger I have

Failed to delete a file in Windows using Java

送分小仙女□ 提交于 2020-04-12 16:48:31
问题 I have been trying to delete a file in windows operating system using the Java IO file.delete() API. However it fails and returns false. The same code works like a charm in Ubuntu. I have verified that the permissions of the file allows the program to delete it. Also all the input and output stream for the file has been opened as try with resources. try (InputStream in = new FileInputStream(localFile); OutputStream out = new FileOutputStream(destinationFileName)) Using a debugger I have

Cancelling input read from io.Reader After Time Limit

ⅰ亾dé卋堺 提交于 2020-04-12 05:12:25
问题 I am wondering how to cancel an input read after a timer expires. I am currently reading from input with fmt.Fscanln(reader, &var) I have a channel that will be filled with a value as soon as the timer expires. I currently have the scan happening on a different go routine. This way, the function that scan's will return when it runs out of time. The problem is if the io.Reader passed to fmt.Fscanln is reading from stdin , it will still wait for input after its goroutine is over. Is there a way

How do I connect io.Reader and io.Writer?

≡放荡痞女 提交于 2020-04-09 17:24:19
问题 I'm writing a long running task which fetch from mongodb (using mgo) multiple times. Then write it to an xlsx file using this module. Then read it again using os.Open then store it to my ftp server. Stor function consume my memory so much, So I think there should be a way not to save file but pass my data from xlsx.Write to ftp.Store directly. (If I can stream simultaneously would be perfect because I don't have to hold all of my documents in server's memory before send them to Stor function)

Java 编程要点之 I/O 流详解

感情迁移 提交于 2020-04-08 12:30:26
原文同步至: http://www.waylau.com/essential-java-io-streams/ 本文详细介绍了 Java I/O 流的基础用法和原理。 字节流(Byte Streams) 字节流处理原始的二进制数据 I/O。输入输出的是8位字节,相关的类为 InputStream 和 OutputStream . 字节流的类有许多。为了演示字节流的工作,我们将重点放在文件 I/O字节流 FileInputStream 和 FileOutputStream 上。其他种类的字节流用法类似,主要区别在于它们构造的方式,大家可以举一反三。 用法 下面一例子 CopyBytes, 从 xanadu.txt 文件复制到 outagain.txt,每次只复制一个字节: public class CopyBytes { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("resources/xanadu.txt"); out = new