Prerequisite – Set character encoding to UTF-8 (if not)
For Windows OS – Set Environment Variables (System Variables) like below
Variable Name: JAVA_TOOL_OPTIONS
Variable Value: -Dfile.encoding=UTF8
For Linux OS – Update profile and add below lines
1) Edit : /etc/profile And add below line
export JAVA_TOOL_OPTIONS='-Dfile.encoding=UTF8'
2) Run below command
source /etc/profile
I found the issue and solution in the below SO link
/questions/73838216/android-java-file-create-java-io-filenotfoundexception-file-name-too-long
But, my main query is related to different o/p for different OS. i.e. why it is working fine on Windows OS and not on Linux OS
Execute the simple program given at last on Windows OS and Linux OS and compare the result.
With the given below file name in Arabic Language with approx. 218 character length,
String fileName = "فيرجى إضافة تعليق ضروري وتعيينه إلىأو_1718270568260 كمرجع مستقبلي هل يمكنك من فضلك إعداد جلسة مع المطورين حول ما يجب التعامل معه وكيف يمكن التعامل معه. في الوقت الحالي إذا كان هناك شيء تحتاج إلى إنجازه من المطورين.msg";
-
On Windows OS – It will create a file at given location
-
On Linux OS – when we executes line
raf = new RandomAccessFile(chunkFile, “rw”);
It will throw below error
java.io.FileNotFoundException (File name too long)
Java Sample Program
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class FileSave {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = null;
String fileName = "فيرجى إضافة تعليق ضروري وتعيينه إلىأو_1718270568260 كمرجع مستقبلي هل يمكنك من فضلك إعداد جلسة مع المطورين حول ما يجب التعامل معه وكيف يمكن التعامل معه. في الوقت الحالي إذا كان هناك شيء تحتاج إلى إنجازه من المطورين.msg";
String fullPath = "D:\test\" + fileName;
try {
File chunkFile = new File(fullPath);
raf = new RandomAccessFile(chunkFile, "rw");
System.out.println(chunkFile.exists() ? "Exist" : "Not Exist");
} catch (FileNotFoundException e) {
System.out.println(e);
} finally {
if(raf != null)raf.close();
}
}
}