[英]Is it smarter to stream in large strings from an external .txt file, or to simply code them directly in?
我正在使用Java,但是我确定这个问题会扩展到其他多种语言和情况。
如果我定期在JTextPane
显示大块文本,
最好将它们写入.txt文件,并定期要求扫描程序读取该文件并将其扔到JTextPane中,
还是我应该直接将文本作为字符串直接写到代码中,然后将其显示在JTextPane中?
一种方法比另一种更合适吗? 我知道“全部写入代码”方法最终会给我带来巨大的方法和一些比我惯用的丑陋代码。 但是,某些情况下需要包含或排除不同的文本位。
如果所需的应用程序使问题更易于回答,我正在使用RPG元素进行基于文本的冒险,以练习Java编码。 根本不专业,只是概念实践。 因此,我必须根据玩家以前的选择,在大块的故事讲述和较小的问题和情况之间进行选择。
我对在编码中处理大量文本是陌生的,因此感谢所有回答!
最好将它放在单独的文件中,最好放在JAR本身中,这样您可以使用getResourceAsStream()
轻松访问它。
然后,您可以轻松编辑此文件而不会弄乱代码,也可以在将应用程序编译为JAR之后对其进行修改。
通常,将逻辑与数据分开是个好习惯。
这是您可能喜欢的FileUtils类中的一些方法:
public static String streamToString(InputStream in) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(in));
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
Log.e(e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
Log.e(e);
}
}
}
return sb.toString();
}
public static InputStream stringToStream(String text) {
try {
return new ByteArrayInputStream(text.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
Log.e(e);
return null;
}
}
public static InputStream getResource(String path) {
return FileUtils.class.getResourceAsStream(path);
}
您还可以使用SimpleConfig
类从文件中解析列表和映射:
(注释掉Log.something调用并替换为System.err.println())
package net.mightypork.rpack.utils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* Utility for parsing simple config files<br>
* # and // mark a comment<br>
* empty lines and lines without "=" are ignored<br>
* lines with "=" must have "key = value" format, or a warning is logged.<br>
* use "NULL" to create empty value.
*
* @author MightyPork
*/
public class SimpleConfig {
/**
* Load list from file
*
* @param file file
* @return map of keys and values
* @throws IOException
*/
public static List<String> listFromFile(File file) throws IOException {
String fileText = FileUtils.fileToString(file);
return listFromString(fileText);
}
/**
* Load map from file
*
* @param file file
* @return map of keys and values
* @throws IOException
*/
public static Map<String, String> mapFromFile(File file) throws IOException {
String fileText = FileUtils.fileToString(file);
return mapFromString(fileText);
}
/**
* Load list from string
*
* @param text text of the file
* @return map of keys and values
*/
public static List<String> listFromString(String text) {
List<String> list = new ArrayList<String>();
String[] groupsLines = text.split("\n");
for (String s : groupsLines) {
// ignore invalid lines
if (s.length() == 0) continue;
if (s.startsWith("#") || s.startsWith("//")) continue;
// NULL value
if (s.equalsIgnoreCase("NULL")) s = null;
if (s != null) s = s.replace("\\n", "\n");
// save extracted key-value pair
list.add(s);
}
return list;
}
/**
* Load map from string
*
* @param text text of the file
* @return map of keys and values
*/
public static Map<String, String> mapFromString(String text) {
LinkedHashMap<String, String> pairs = new LinkedHashMap<String, String>();
String[] groupsLines = text.split("\n");
for (String s : groupsLines) {
// ignore invalid lines
if (s.length() == 0) continue;
if (s.startsWith("#") || s.startsWith("//")) continue;
if (!s.contains("=")) continue;
// split and trim
String[] parts = s.split("=");
for (int i = 0; i < parts.length; i++) {
parts[i] = parts[i].trim();
}
// check if both parts are valid
if (parts.length == 0) {
Log.w("Bad line in config file: " + s);
continue;
}
if (parts.length == 1) {
parts = new String[] { parts[0], "" };
}
if (parts.length != 2) {
Log.w("Bad line in config file: " + s);
continue;
}
// NULL value
if (parts[0].equalsIgnoreCase("NULL")) parts[0] = null;
if (parts[1].equalsIgnoreCase("NULL")) parts[1] = null;
if (parts[0] != null) parts[0] = parts[0].replace("\\n", "\n");
if (parts[1] != null) parts[1] = parts[1].replace("\\n", "\n");
// save extracted key-value pair
pairs.put(parts[0], parts[1]);
}
return pairs;
}
/**
* Save map to file
*
* @param target
* @param data
* @throws IOException
*/
public static void mapToFile(File target, Map<String, String> data) throws IOException {
String text = ""; //# File written by SimpleConfig
for (Entry<String, String> e : data.entrySet()) {
if (text.length() > 0) text += "\n";
String key = e.getKey();
String value = e.getValue();
if (key == null) key = "NULL";
if (value == null) value = "NULL";
key = key.replace("\n", "\\n");
value = value.replace("\n", "\\n");
text += key + " = " + value;
}
FileUtils.stringToFile(target, text);
}
/**
* Save list to file
*
* @param target
* @param data
* @throws IOException
*/
public static void listToFile(File target, List<String> data) throws IOException {
String text = ""; //# File written by SimpleConfig
for (String s : data) {
if (text.length() > 0) text += "\n";
if (s == null) s = "NULL";
s = s.replace("\n", "\\n");
text += s;
}
FileUtils.stringToFile(target, text);
}
}
最好有单独的文件。 原因之一是您希望能够将游戏翻译成其他语言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.