I have previously written a code where I have added a time stamp to a file once it has been save in a directory.
Now I wanna to be able to truncate the time stamp from the file which comes after the extension .txt
note that my time stamp format is:_yyyy-mm-dd.
If you have the date after your extension in the form _yyyy-mm-dd just can use
String strippedFileName = fileName.substring(0, fileName.length() - 11);
or a bit nicer
String dateFormatString = "_yyyy-mm-dd";
String strippedFileName = fileName.substring(0, fileName.length() - dateFormatString.length());
You could also do this:
String trimmed = filename.replaceAll(".{11}$", "");
Related
When I encode tex, for some reason it cuts off part of the string ... What could be the problem?
DateFormat dateFormat =
new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a", Locale.ENGLISH);
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR,+ 9);
String server_time = dateFormat.format(calendar.getTime());
String wmsAuthSign = "server_time=" + server_time + "&hash_value=U2QK9TLB55JWTZr3OKZHtg==&validminutes=120";
wmsAuthSign = "?wmsAuthSign=" + Base64.encodeToString(wmsAuthSign.getBytes(), Base64.DEFAULT);
I am submitting something like this:
server_time=02/18/2019 23:38:43 PM&hash_value=U2QK9TLB55JWTZr3OKZHtg==&validminutes=120
And if you decode the encoded text, you get a trimmed result:
server_time=02/18/2019 23:38:43 PM&hash_value=U2QK9TLB55J
Because of RFC-2045:
(5) (Soft Line Breaks) The Quoted-Printable encoding
REQUIRES that encoded lines be no more than 76
characters long. If longer lines are to be encoded
with the Quoted-Printable encoding, "soft" line breaks
source data string:
server_time=02/18/2019 23:38:43 PM&hash_value=U2QK9TLB55JWTZr3OKZHtg==&validminutes=120
Base64 encoded to string:
c2VydmVyX3RpbWU9MDIvMTgvMjAxOSAyMzoxMjo1NiBQTSZoYXNoX3ZhbHVlPVUyUUs5VExCNTVK
V1RacjNPS1pIdGc9PSZ2YWxpZG1pbnV0ZXM9MTIw
exactly like it shown above: with line break. But on receiver side you probably decode only first line
c2VydmVyX3RpbWU9MDIvMTgvMjAxOSAyMzoxMjo1NiBQTSZoYXNoX3ZhbHVlPVUyUUs5VExCNTVK
that is server_time=02/18/2019 23:12:21 PM&hash_value=U2QK9TLB55J
So decode on receiver side whole received data, not only first line.
Or you may be sent to receiver side only first line of encoded Base64.
Also take a look at this answer of Mohammad Adil:
On android, Use Base64.NO_WRAP instead of Base64.DEFAULT
String filename = uri.getPath();
Gives me this string file:///storage/emulated/0/.temp/file-2092620235jpg
What is the best way to add a dot before file extension?
If you know for sure that the file extension is three letters long (so, no docx or similar), you can always try manipulating string in this way:
int length = filename.length();
String result = filename.substring(0, length - 3) + "." + filename.substring(length - 3);
I have a directory which contains files in the following format. The files are in a diretory called /incoming/external/data
ABC_20100806.csv
ABC_20100807.csv
ABC_20100808.csv
ABC_20100809.csv
ABC_20100810.csv
ABC_20100811.csv
ABC_20100812.csv
As you can see the filename of the file includes a timestamp. i.e. [RANGE]_[YYYYMMDD].csv
What i need to do is find out which of these files has the newest date using the timestamp on the filename not the system timestamp and store the filename in a variable and move it to another directory and move the rest to a different directory in java.
You can read the filenames into an array using:
File directory = new File("/incoming/external/data");
String[] fileNames = directory.list(new FilenameFilter() {
public boolean accept(File dir, String fileName) {
return fileName.endsWith(".csv");
}
});
And from there simply sort the array if your files always have the same prefix:
Arrays.sort(fileNames);
One way you can remove the prefix and suffix of each fileName to extract the date is:
int underline = fileName.indexOf("_");
int dot = fileName.indexOf(".");
String datePart = fileName.substring(underline, dot);
And then you can add that string to an array and sort (lexically).
If for some other reason you want to convert the dates into Java Dates, you can use:
SimpleDateFormat dt = new SimpleDateFormat("yyyymmdd");
Date date = dt.parse(datepart);
And you'll have a Java date, which you can also sort in an array or list.
I have a servlet witch is generating a .xls file and then it's sending the generated file to the user for download with the code below:
// Write the output to a file
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter=
new SimpleDateFormat("yyyy_MMM_dd");
String dateNow = formatter.format(currentDate.getTime());
String path = "webapps/myapp/exports/";
String fileName = ("Table_export_"+ dateNow + ".xls");
FileOutputStream fileOut = new FileOutputStream(path+fileName);
wb.write(fileOut);
fileOut.close();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment;filename="+fileName);
The file is saved on the server with a size of 5 kb, but after the browser dialog and after choosing save or open, the file is empty and the size is 0 kb. I can not understand what is wrong.
I'm fighting with this issue for about 2 hours without a success. I have tried to set the full path:
response.setHeader("Content-Disposition","attachment;filename="path+fileName);
But I got a document with a strange name and it also was 0 kb.
I'm pretty sure that I'm missing something really small, but as a junior developer I still can not figure it out.
Since you already have a method that can write your file to an output stream you can simply change your code this way to send it to the user :
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MMM_dd");
String dateNow = formatter.format(currentDate.getTime());
String fileName = ("Table_export_"+ dateNow + ".xls");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment;filename="+fileName);
OutputStream outputStream = response.getOutputStream();
try {
wb.write(outputStream);
} finally {
outputStream.close();
}
It won't be saved on your server HDD though.
The method HttpServletResponse#getOutputStream() allows you to access the stream of bytes that will be sent back to the client.
When you used the format -
String path = "webapps/myapp/exports/";
String fileName = ("Table_export_"+ dateNow + ".xls");
...
response.setHeader("Content-Disposition","attachment;filename="path+fileName);
Was the filename "webapps%2Fmyapps%2Fexports%2fTable_export_....xls"?
Als the filesize would have been zero because you are not putting any data in the file
I'm looking for a way to parse a string which would replace some patterns with components of a date that I provide.
Do you know of a standard way to do this?
One usage would be:
parseForDate("fileName%YYYY%MM.csv", new Date()); // returns: filename201301.csv
Best regards
Consider using format:
String filename = String.format("fileName%1$tY%1$tm.csv", new Date());
String filename = "fileName%" + new SimpleDateFormat("yyyy%MM").format(new Date())+".csv"; // returns: filename201301.csv