Modify the content of a file using Java

I would start with closing reader, and flushing writer: public class FileReplace { List<String> lines = new ArrayList<String>(); String line = null; public void doIt() { try { File f1 = new File(“d:/new folder/t1.htm”); FileReader fr = new FileReader(f1); BufferedReader br = new BufferedReader(fr); while ((line = br.readLine()) != null) { if (line.contains(“java”)) line = … Read more

How to make a folder hidden using java

If you’re using Java 7 you can use the new java.nio.file.attribute package like so: Path path = FileSystems.getDefault().getPath(“/j”, “sa”); Files.setAttribute(path, “dos:hidden”, true); See more info at http://download.oracle.com/javase/tutorial/essential/io/fileAttr.html Or, if you’re using an older version of Java and/or want to do it using Runtime, try this: Process process = Runtime.getRuntime().exec(“cmd.exe /C attrib -s -h -r your_path”); … Read more

How to read and write a STL C++ string?

You are trying to mix C style I/O with C++ types. When using C++ you should use the std::cin and std::cout streams for console input and output. #include <string> #include <iostream> … std::string in; std::string out(“hello world”); std::cin >> in; std::cout << out; But when reading a string std::cin stops reading as soon as it … Read more

open file in exclusive mode in C#

What you are doing is the right thing. Probably you are just testing it incorrectly. You should open it with a program that locks the file when it’s open. Notepad wouldn’t do. You can run your application twice to see: static void Main(string[] args) { // Make sure test.txt exists before running. Run this app … Read more