Create Text File Without BOM

Well it writes the BOM because you are instructing it to, in the line Encoding utf8WithoutBom = new UTF8Encoding(true); true means that the BOM should be emitted, using Encoding utf8WithoutBom = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false); writes no BOM. My objective is create a file using UTF-8 as Encoding and 8859-1 as CharSet Sadly, this is not … Read more

Java replace specific string in text file

You could create a string of total file content and replace all the occurrence in the string and write to that file again. You could something like this: File log= new File(“log.txt”); String search = “textFiles/a.txt”; String replace = “replaceText/b.txt”; try{ FileReader fr = new FileReader(log); String s; String totalStr = “”; try (BufferedReader br … Read more

Simplest way to encrypt a text file in java

Try this,… Its pretty simple import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; public class HelloWorld{ public static void main(String[] args) { try{ KeyGenerator keygenerator = KeyGenerator.getInstance(“DES”); SecretKey myDesKey = keygenerator.generateKey(); Cipher desCipher; desCipher = Cipher.getInstance(“DES”); byte[] text = “No body can see me.”.getBytes(“UTF8”); desCipher.init(Cipher.ENCRYPT_MODE, myDesKey); byte[] textEncrypted = desCipher.doFinal(text); String s = new String(textEncrypted); System.out.println(s); desCipher.init(Cipher.DECRYPT_MODE, … Read more

Ban IPs from text file using htaccess [closed]

You can try using variations of RewriteMap. You’ll need access to the server/vhost config because that directive only works there. You can then use the map inside htaccess files. Say your blacklist.txt file looks like this: 111.222.33.44 deny 55.66.77.88 deny 192.168.0.1 allow You can define the map like so: RewriteEngine On RewriteMap access txt:/path/to/blacklist.txt Then … Read more

Copying from one text file to another using Python

The oneliner: open(“out1.txt”, “w”).writelines([l for l in open(“in.txt”).readlines() if “tests/file/myword” in l]) Recommended with with: with open(“in.txt”) as f: lines = f.readlines() lines = [l for l in lines if “ROW” in l] with open(“out.txt”, “w”) as f1: f1.writelines(lines) Using less memory: with open(“in.txt”) as f: with open(“out.txt”, “w”) as f1: for line in f: … Read more

Open a text file in the default text editor… via Java?

You can do that with: java.awt.Desktop.getDesktop().edit(file); This links to the tutorial article on java.awt.Desktop: Java™ Standard Edition version 6 narrows the gap between performance and integration of native applications and Java applications. Along with the new system tray functionality, splash screen support, and enhanced printing for JTables , Java SE version 6 provides the Desktop … Read more

How to read a text file directly from Internet using Java?

Use an URL instead of File for any access that is not on your local computer. URL url = new URL(“http://www.puzzlers.org/pub/wordlists/pocket.txt”); Scanner s = new Scanner(url.openStream()); Actually, URL is even more generally useful, also for local access (use a file: URL), jar files, and about everything that one can retrieve somehow. The way above interprets … Read more