Here you can find the source of cleanFileName(String sText)
Parameter | Description |
---|---|
sText | String, the text to clean |
public static String cleanFileName(String sText)
//package com.java2s; /******************************************************************************** * * * (c) Copyright 2010 Verizon Communications USA and The Open University UK * * * * This software is freely distributed in accordance with * * the GNU Lesser General Public (LGPL) license, version 3 or later * * as published by the Free Software Foundation. * * For details see LGPL: http://www.fsf.org/licensing/licenses/lgpl.html * * and GPL: http://www.fsf.org/licensing/licenses/gpl-3.0.html * * * * This software is provided by the copyright holders and contributors "as is" * * and any express or implied warranties, including, but not limited to, the * * implied warranties of merchantability and fitness for a particular purpose * * are disclaimed. In no event shall the copyright owner or contributors be * * liable for any direct, indirect, incidental, special, exemplary, or * * consequential damages (including, but not limited to, procurement of * * substitute goods or services; loss of use, data, or profits; or business * * interruption) however caused and on any theory of liability, whether in * * contract, strict liability, or tort (including negligence or otherwise) * * arising in any way out of the use of this software, even if advised of the * * possibility of such damage. * * * ********************************************************************************/ public class Main { /**//from ww w . j av a 2 s . c om * Clean illegal file name characters from the given text * N.B. you must pass in the file name only, without the path. * * @param sText String, the text to clean * @return String, the clean text */ public static String cleanFileName(String sText) { if (sText == null || sText.equals("")) return ""; sText = replace(sText, ' ', "_"); sText = replace(sText, '"', ""); sText = replace(sText, '\'', ""); sText = replace(sText, '\\', ""); sText = replace(sText, '/', ""); sText = replace(sText, '<', ""); sText = replace(sText, '>', ""); sText = replace(sText, '|', ""); sText = replace(sText, '?', ""); sText = replace(sText, '*', ""); sText = replace(sText, ':', ""); // Linux did not like ... in a file name sText = replace(sText, "....", ""); sText = replace(sText, "...", ""); sText = replace(sText, "..", ""); return sText; } /** * Replace String a, in the given text with String b * * @param text the text to replace character in. * @param a the string to replace * @param b the string to replace the character with. * * @return String, the replaced text. */ public static String replace(String text, String a, String b) { int length = text.length(); int lenA = a.length(); int lenB = b.length(); int gofrom = 0; boolean goon = true; while (goon) { int next = text.indexOf(a, gofrom); if (next != -1) { if (next + lenA > length) { text = text.substring(0, next) + b; } else { text = text.substring(0, next) + b + text.substring(next + lenA); } gofrom = next + lenB; } else { goon = false; } length = text.length(); if (gofrom > length) goon = false; } return text; } /** * Replace char a, in the given text with String b * * @param text the text to replace character in. * @param a the character to replace * @param b the string to replace the character with. * * @return String, the replaced text. */ public static String replace(String text, char a, String b) { int length = text.length(); int len = b.length(); int gofrom = 0; boolean goon = true; while (goon) { int next = text.indexOf(a, gofrom); if (next != -1) { if (next + 1 > length) text = text.substring(0, next) + b; else text = text.substring(0, next) + b + text.substring(next + 1); gofrom = next + len; } else goon = false; length = text.length(); if (gofrom > length) goon = false; } return text; } }