Java examples for java.lang:String New Line
Replaces OS specific illegal characters from any filename with '_', including ( / \n \r \t ) on all operating systems, ( ?
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.lang.reflect.Method; import java.net.URL; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Properties; public class Main{ public static void main(String[] argv) throws Exception{ String name = "java2s.com"; System.out.println(convertFileName(name)); }/*w ww.j a va 2s. c o m*/ /** * Variable for whether or not we're on Windows. */ private static boolean _isWindows = false; /** * Variable for whether or not we're on MacOSX. */ private static boolean _isMacOSX = false; /** * Variable for whether or not we're on Linux. */ private static boolean _isLinux = false; /** * Variable for whether or not we're on Solaris. */ private static boolean _isSolaris = false; /** * Variable for whether or not we're on OS/2. */ private static boolean _isOS2 = false; /** * Several arrays of illegal characters on various operating systems. * Used by convertFileName */ private static final char[] ILLEGAL_CHARS_ANY_OS = { '/', '\n', '\r', '\t', '\0', '\f' }; private static final char[] ILLEGAL_CHARS_UNIX = { '`' }; private static final char[] ILLEGAL_CHARS_WINDOWS = { '?', '*', '\\', '<', '>', '|', '\"', ':' }; private static final char[] ILLEGAL_CHARS_MACOS = { ':' }; /** * Replaces OS specific illegal characters from any filename with '_', * including ( / \n \r \t ) on all operating systems, ( ? * \ < > | " ) * on Windows, ( ` ) on unix. * * @param name the filename to check for illegal characters * @return String containing the cleaned filename */ public static String convertFileName(String name) { // ensure that block-characters aren't in the filename. name = I18NConvert.instance().compose(name); // if the name is too long, reduce it. We don't go all the way // up to 256 because we don't know how long the directory name is // We want to keep the extension, though. if (name.length() > 180) { int extStart = name.lastIndexOf('.'); if (extStart == -1) { // no extension, wierd, but possible name = name.substring(0, 180); } else { // if extension is greater than 11, we concat it. // ( 11 = '.' + 10 extension characters ) int extLength = name.length() - extStart; int extEnd = extLength > 11 ? extStart + 11 : name.length(); name = name.substring(0, 180 - extLength) + name.substring(extStart, extEnd); } } for (int i = 0; i < ILLEGAL_CHARS_ANY_OS.length; i++) name = name.replace(ILLEGAL_CHARS_ANY_OS[i], '_'); if (_isWindows || _isOS2) { for (int i = 0; i < ILLEGAL_CHARS_WINDOWS.length; i++) name = name.replace(ILLEGAL_CHARS_WINDOWS[i], '_'); } else if (_isLinux || _isSolaris) { for (int i = 0; i < ILLEGAL_CHARS_UNIX.length; i++) name = name.replace(ILLEGAL_CHARS_UNIX[i], '_'); } else if (_isMacOSX) { for (int i = 0; i < ILLEGAL_CHARS_MACOS.length; i++) name = name.replace(ILLEGAL_CHARS_MACOS[i], '_'); } return name; } }