⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testfileutil.java

📁 Jodd是一个开源的公用Java基础类库
💻 JAVA
字号:
package jodd.file;

import junit.framework.TestCase;

public class TestFileUtil extends TestCase {
	
	protected final String dataRoot = "modules/testcase/data";

	public void testObjSerialization() {
		SimpleBean sb = new SimpleBean();
		assertEquals(3, sb.getFoo());
		try {
			FileUtil.writeObject(dataRoot + "/sb.dat", sb);
		} catch (Exception ex) {
			fail("FileUtil.writeObject " + ex.toString());
		}

		try {
			Object o = FileUtil.readObject(dataRoot + "/sb.dat");
			SimpleBean sb2 = (SimpleBean) o;
			assertEquals(3, sb2.getFoo());
		} catch (Exception ex) {
			fail("FileUtil.readObject " + ex.toString());
		}
	}


	public void testFileManipulation() {

		try {
			FileUtil.copy(dataRoot + "/sb.dat", dataRoot + "/sb1.dat");
		} catch (Exception ex) {
			fail("FileUtil.copy " + ex.toString());
		}
	}


	public void testString() {
		String s = "This is a test file\nIt only has\nthree lines!!";

		try {
			FileUtil.writeString(dataRoot + "/test.txt", s);
		} catch(Exception ex) {
			fail("FileUtil.writeString " + ex.toString());
		}

		String s2 = null;
		try {
			s2 = FileUtil.readString(dataRoot + "/test.txt");
		} catch(Exception ex) {
			fail("FileUtil.readString " + ex.toString());
		}

		assertEquals(s, s2);

		// test unicode chars (i.e. greaer then 255)
		char buf[] = s.toCharArray();
		buf[0] = 256;
		s = new String(buf);

		try {
			FileUtil.writeString(dataRoot + "/test.txt", s);
		} catch(Exception ex) {
			fail("FileUtil.writeString " + ex.toString());
		}

		try {
			s2 = FileUtil.readString(dataRoot + "/test.txt");
		} catch(Exception ex) {
			fail("FileUtil.readString " + ex.toString());
		}

		assertEquals(s.substring(1), s2.substring(1));
		assertTrue(s.charAt(0) != s2.charAt(0));
	}

	public void testUnicodeString() {
		String s = "This is a test file\nIt only has\nthree lines!!";

		char buf[] = s.toCharArray();
		buf[0] = 256;
		s = new String(buf);

		try {
			FileUtil.writeString(dataRoot + "/test2.txt", s, "UTF-16");
		} catch(Exception ex) {
			fail("FileUtil.writeString " + ex.toString());
		}

		String s2 = null;

		try {
			s2 = FileUtil.readString(dataRoot + "/test2.txt", "UTF-16");
		} catch(Exception ex) {
			fail("FileUtil.readString " + ex.toString());
		}
		assertEquals(s, s2);

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -