file-handling

Modifying a text file in a ZIP archive in Java

眉间皱痕 提交于 2019-11-26 21:29:38
问题 My use case requires me to open a txt file, say abc.txt which is inside a zip archive which contains key-value pairs in the form key1=value1 key2=value2 .. and so on where each key-value pair is in a new line. I have to change one value corresponding to a certain key and put the text file back in a new copy of the archive. How do I do this in java? My attempt so far: ZipFile zipFile = new ZipFile("test.zip"); final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("out.zip"));

How to check if a file is empty in Bash?

百般思念 提交于 2019-11-26 17:57:47
问题 I have a file called diff.txt. Want to check if it is empty. Did something like this but couldn't get it working. if [ -s diff.txt ] then touch empty.txt rm full.txt else touch full.txt rm emtpy.txt fi 回答1: Misspellings are irritating, aren't they? Check your spelling of empty , but then also try this: #!/bin/bash -e if [ -s diff.txt ] then rm -f empty.txt touch full.txt else rm -f full.txt touch empty.txt fi I like shell scripting a lot, but one disadvantage of it is that the shell cannot

How can I download a file from a URL and save it in Rails?

隐身守侯 提交于 2019-11-26 16:53:42
I have a URL to an image which i want to save locally, so that I can use Paperclip to produce a thumbnail for my application. What's the best way to download and save the image? (I looked into ruby file handling but did not come across anything.) Levi Try this: require 'open-uri' open('image.png', 'wb') do |file| file << open('http://example.com/image.png').read end Clemens Helm An even shorter version: require 'open-uri' download = open('http://example.com/image.png') IO.copy_stream(download, '~/image.png') To keep the same filename: IO.copy_stream(download, "~/#{download.base_uri.to_s.split(

Return value of assignment operation in Java

故事扮演 提交于 2019-11-26 16:42:58
I encountered a statement in Java while ((line = reader.readLine()) != null) { out.append(line); } How do assignment operations return a value in Java? The statement we are checking is line = reader.readLine() and we compare it with null . Since readLine will return a string, how exactly are we checking for null ? The assignment operator in Java returns the assigned value (like it does, e.g., in c ). So here, readLine() will be executed, and it's return value stored in line . That value is then checked against null , and if it is null , the loop will terminate. Assignment expressions are

How to read text file by particular line separator character?

假装没事ソ 提交于 2019-11-26 16:05:47
问题 Reading a text file using streamreader. using (StreamReader sr = new StreamReader(FileName, Encoding.Default)) { string line = sr.ReadLine(); } I want to force that line delimiter should be \n not \r . So how can i do that? 回答1: string text = sr.ReadToEnd(); string[] lines = text.Split('\r'); foreach(string s in lines) { // Consume } 回答2: I would implement something like George's answer, but as an extension method that avoids loading the whole file at once (not tested, but something like this

How to Find And Replace Text In A File With C#

≯℡__Kan透↙ 提交于 2019-11-26 15:45:41
My code so far StreamReader reading = File.OpenText("test.txt"); string str; while ((str = reading.ReadLine())!=null) { if (str.Contains("some text")) { StreamWriter write = new StreamWriter("test.txt"); } } I know how to find the text, but I have no idea on how to replace the text in the file with my own. Read all file content. Make a replacement with String.Replace . Write content back to file. string text = File.ReadAllText("test.txt"); text = text.Replace("some text", "new value"); File.WriteAllText("test.txt", text); You're going to have a hard time writing to the same file you're reading

Write text files without Byte Order Mark (BOM)?

此生再无相见时 提交于 2019-11-26 15:17:07
I am trying to create a text file using VB.Net with UTF8 encoding, without BOM. Can anybody help me, how to do this? I can write file with UTF8 encoding but, how to remove Byte Order Mark from it? edit1: I have tried code like this; Dim utf8 As New UTF8Encoding() Dim utf8EmitBOM As New UTF8Encoding(True) Dim strW As New StreamWriter("c:\temp\bom\1.html", True, utf8EmitBOM) strW.Write(utf8EmitBOM.GetPreamble()) strW.WriteLine("hi there") strW.Close() Dim strw2 As New StreamWriter("c:\temp\bom\2.html", True, utf8) strw2.Write(utf8.GetPreamble()) strw2.WriteLine("hi there") strw2.Close() 1.html

Why is my iOS app not showing up in other apps&#39; “Open in” dialog?

我们两清 提交于 2019-11-26 15:14:11
问题 I am trying to implement the registering process that allows my iOS app to show up in the "Open in" list of other applications (as described in Apple's Document Interaction Programming Topics). I want my app to be able to handle audio from any app that will provide a standard audio file format (MP3, AIFF, WAV, etc.). As I understand it, all I should need to do is add the CFBundleDocumentTypes key, with relevant subdata, to my app's Info.plist. This is what I put in (via Xcode 4's Document

How to read line by line or a whole text file at once?

拈花ヽ惹草 提交于 2019-11-26 15:06:40
I'm in a tutorial which introduces files (how to read and write from\to file) First of all, this is not a homework, this is just general help I'm seeking. I know how to read one word at a time, but I don't know how to read one line at a time or how to read the whole text file. What if my file contains 1000 words? It is not practical to read each word. My text file named (Read) contains the following: I love to play games I love reading I have 2 books This is what I have accomplished so far: #include <iostream> #include <fstream> using namespace std; int main (){ ifstream inFile; inFile.open(

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process

心不动则不痛 提交于 2019-11-26 14:37:26
问题 My code is for a script that looks at a folder and deletes images that are under a resolution of 1920x1080. The problem I am having is that when my code runs; import os from PIL import Image while True: img_dir = r"C:\Users\Harold\Google Drive\wallpapers" for filename in os.listdir(img_dir): filepath = os.path.join(img_dir, filename) im = Image.open(filepath) x, y = im.size totalsize = x*y if totalsize < 2073600: os.remove(filepath) I get this error message: Traceback (most recent call last):