I have a file which contains sql statements.
Now in this file, I want to add a word "collate" after every 'char()' and 'varchar()' in the file.
How do you do that?
Iterate through the file line by line. On each String do two replaceAll( ... ) using your Strings above. Then write each line into a new File. When done, rename the original file to some back-up name and rename the new file to the original file's name.
Edit 1
I just noticed your javascript tag. So what type of problem is this, Java or Javascript?
Related
I have it like this:
file=new File(pathString+"/TileRecordings/",jTextField1.getText()+".txt");
and then after null checking etc...
file.createNewFile();
Thing is, if the texfield value was 'test' it would create 'text.txt' AS A FOLDER. Not an actual text file.
Is this a Ubuntu only thing? How do I force it to literally create a text file, not a folder named 'test.txt'.
Maybe try features from java.nio.file
for example:
Path temp = Paths.get(pathString+"/TileRecordings/"+jTextField1.getText()+".txt");
Files.write(temp, contentToSave.toString().getBytes());
I am having a file name like suppose abcde-1_Transformed.xml.
Now if I have many number of files suppose in the above file name the '1' which is present has files like
abcde-2_Transformed.xml.
abcde-3_Transformed.xml.
abcde-4_Transformed.xml.
till abcde-1966_Transformed.xml.
How to get the file names which are not present in the total number of files using regex ?
I think rather than making a regex you can just read the file in the current directory using this link Read all files in a folder
Then parse out the number from file name and check if it's present or not.
i read on several posts that we for deleting a file through java which has spaces in the name, i can use delete() method (Java 6). eg:
File f = new File("/mnt/test ex.txt");
f.delete();
but when I'm making a file object like this () :
StringBuilder fullFileName = "C:/Temp_Folder\week month.xlsx";
fileToRead = new File(fullFileName.toString());
fileToRead.delete();
I'm not able to do so and i get the following exception :
java.io.FileNotFoundException: "C:\Temp_Folder\week month.xlsx" (The filename, directory name, or volume label syntax is incorrect)
What am i missing here?
P.s. : I tried using quotes on the filename as well without success
fileToRead = new File('"'+fullFileName.toString()+'"');
Edit : I've edited the quotes on the stringBuilder (a type from my end). Actually the StringBuilder object is a parameter and we are appending objects to fetch the actual name. I just gave you the final declaration.
As far as week month.xlsx goes, that is the name of the file and not two different variables (which means the filename DOES have spaces in between; it could be something like
Name with spaces.xlsx
Thanks for the quick turnaround everyone.
You do NOT need a specific treatment for file names with spaces in Java -- or any other programming language with a file access API for that matter.
Do not mix Java with a command interpreter.
In your case, your File should be declared as:
new File("C:\\Temp_Folder\\name with spaces.xlsx")
and that's it.
If Java reports a FileNotFoundException then there is a problem. Unfortunately, the File API is broken and this exception can be thrown if the file exists but you cannot read it, for instance. Have a look at the complete stack trace.
Do yourself a favour: use Java 7 and the new Files API. With this API, exceptions actually make some sense -- and a delete operation will not "silently" fail either.
As to building the filename itself, you can for example use String.format():
final String filename = String.format("C:\\Temp_Folder\\%s %s.xlsx", month, week);
According to the exception:
java.io.FileNotFoundException: "C:\Temp_Folder\week month.xlsx"
You are looking for the following file:
"C:\Temp_Folder\week month.xlsx"
Note the quotes! This file does not exist.
You will have to modify your code to ensure that your file name does not include the surrounding quotes (not needed).
I.e. (Assuming java 6 here)
File file = new File("C:\\Temp_Folder\\week month.xlsx");
file.delete();
Note, the backslash is an escape character hence it is doubled in the string.
I need to pick a text file from a folder and read the data in the file. The file is generated dynamically in that folder in the format "XXX_2010-12-06". So, first i need to check if the file is existing in the folder and if it exists the content of the file should be read.
I have the code to read the content in the text file. I need to provide the path of the file
Can anyone help me in coding this using java...
You can create a new instance of File and initiate it with the path to the file itself.
File file = new File("/a/path/to/a/file/TheFile.txt");
Once you have your File instance created you can check to see if it exists by calling the exists() method inside of File.
System.out.println(file.exists() ? "The file exists!" : "The file doesn't exist!");
I couldn't really understand what you were asking for help with. But if you edit your question to be more clear I will edit my answer to fulfill further answering.
The File class has methods for checking whether a file exists.
Scenario:
I save my drawing with a file name as picture. After a while I made some changes on the file picture and save it again.
Since both file have the same name, is it possible that the new file automatically saved as picture1 without need to manually change the file name in the program? ... I means automatically add number at the end of the file name if the file have the same name?
so at the end if I made changes on the files so many times, I will have many files named as picture, picture1, picture2, picture3...
You could use the create temp file method for this, use:
as prefix the basename of your file, in this case it would be "picture"
as suffix the image type, for instance ".png"
The file created will be unique by definition.
Another way is to create a unique filename based on the current time, as in:
SimpleDateFormat fmt = new SimpleDateFormat("picture_yyyyMMdd_HHmmss.png");
String filename = fmt.format(new Date());
This would give you meaningfull filenames with regards to edit history.
Sure, if you program it so. If your desired filename exists check to see if a file with the same name, with an increasing integer starting at 1, exists. Once you find one that doesn't exist, use it as the name. Make sure to do the right thing with file extensions (you probably want file2.txt, not file.txt.2).
if filename exists
{
loop suffix from 1 to some limit
{
if filename + suffix doesn't exist
{
exit loop and use this name
}
}
}