Delete Files with same Prefix String using Java

No, you can’t. Java is rather low-level language — comparing with shell-script — so things like this must be done more explicetly. You should search for files with required mask with folder.listFiles(FilenameFilter), and iterate through returned array deleting each entry. Like this: final File folder = … final File[] files = folder.listFiles( new FilenameFilter() { … Read more

Get file name from a file location in Java

new File(fileName).getName(); or int idx = fileName.replaceAll(“\\\\”, “https://stackoverflow.com/”).lastIndexOf(“https://stackoverflow.com/”); return idx >= 0 ? fileName.substring(idx + 1) : fileName; Notice that the first solution is system dependent. It only takes the system’s path separator character into account. So if your code runs on a Unix system and receives a Windows path, it won’t work. This is … Read more

Creating a thread-safe temporary file name

Dir::Tmpname.create You could use Dir::Tmpname.create. It figures out what temporary directory to use (unless you pass it a directory). It’s a little ugly to use given that it expects a block: require ‘tmpdir’ # => true Dir::Tmpname.create([‘prefix-‘, ‘.ext’]) {} # => “/tmp/prefix-20190827-1-87n9iu.ext” Dir::Tmpname.create([‘prefix-‘, ‘.ext’], ‘/my/custom/directory’) {} # => “/my/custom/directory/prefix-20190827-1-11x2u0h.ext” The block is there for code … Read more

Create file name using date and time

To create a file named the current date/time: Date date = new Date() ; SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd HH-mm-ss”) ; File file = new File(dateFormat.format(date) + “.tsv”) ; BufferedWriter out = new BufferedWriter(new FileWriter(file)); out.write(“Writing to file”); out.close();

Base64 Encoding safe for filenames?

Modified Base64 (when /,= and + are replaced) is safe to create names but does not guarantee reverse transformation due to case insensitivity of many file systems and urls. Base64 is case sensitive, so it will not guarantee 1-to-1 mapping in cases of case insensitive file systems (all Windows files systems, ignoring POSIX subsystem cases). … Read more

Open file by filename wildcard

import os import re path = “/home/mypath” for filename in os.listdir(path): if re.match(“text\d+.txt”, filename): with open(os.path.join(path, filename), ‘r’) as f: for line in f: print line, Although you ignored my perfectly fine solution, here you go: import glob path = “/home/mydir/*.txt” for filename in glob.glob(path): with open(filename, ‘r’) as f: for line in f: print … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)