java utils类_Java Utils类代码示例

import it.neokree.materialnavigationdrawer.util.Utils; //导入依赖的package包/类

public MaterialSubheader(Context ctx) {

float density = ctx.getResources().getDisplayMetrics().density;

// create layout

LinearLayout layout = new LinearLayout(ctx);

layout.setOrientation(LinearLayout.VERTICAL);

// inflate the line

View view = new View(ctx);

view.setBackgroundColor(Color.parseColor("#8f8f8f"));

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,1);

params.setMargins(0,(int) (8 * density), 0 , (int) (8 * density));

layout.addView(view,params);

// inflate the text

text = new TextView(ctx);

Utils.setAlpha(text,0.54f);

text.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);

text.setGravity(Gravity.START);

LinearLayout.LayoutParams paramsText = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

paramsText.setMargins((int) (16 * density),0, (int) (16 * density) , (int) (4 * density));

layout.addView(text,paramsText);

this.view = layout;

// get attributes from current theme

Resources.Theme theme = ctx.getTheme();

TypedValue typedValue = new TypedValue();

theme.resolveAttribute(R.attr.sectionStyle,typedValue,true);

TypedArray values = theme.obtainStyledAttributes(typedValue.resourceId,R.styleable.MaterialSection);

try {

titleColor = values.getColor(R.styleable.MaterialSubheader_subheaderTitleColor,0x000);

}

finally {

values.recycle();

}

// set attributes to the view

text.setTextColor(Color.BLACK);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java API 中并没有直接提供 tar 文件的压缩和解压缩功能,但是我们可以通过使用 `java.util.zip` 包中的 `ZipOutputStream` 和 `ZipEntry` 来实现对 tar 文件的压缩,同时也可以使用 Apache Commons Compress 库中的 `TarArchiveInputStream` 和 `TarArchiveOutputStream` 来实现对 tar 文件的压缩和解压缩。 以下是使用 Apache Commons Compress 库中的 `TarArchiveInputStream` 和 `TarArchiveOutputStream` 来实现 tar 文件的压缩和解压缩的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; public class TarUtil { public static void createTarFile(String[] sourceFilePaths, String tarFilePath) throws IOException { try (TarArchiveOutputStream tarOs = new TarArchiveOutputStream(new FileOutputStream(tarFilePath))) { for (String sourceFilePath : sourceFilePaths) { File file = new File(sourceFilePath); TarArchiveEntry entry = new TarArchiveEntry(file, file.getName()); tarOs.putArchiveEntry(entry); try (FileInputStream fileIs = new FileInputStream(file)) { byte[] buffer = new byte[4096]; int read = 0; while ((read = fileIs.read(buffer)) != -1) { tarOs.write(buffer, 0, read); } } tarOs.closeArchiveEntry(); } } } public static void extractTarFile(String tarFilePath, String destDirectory) throws IOException { try (TarArchiveInputStream tarIs = new TarArchiveInputStream(new FileInputStream(tarFilePath))) { TarArchiveEntry entry = null; while ((entry = tarIs.getNextTarEntry()) != null) { String entryName = entry.getName(); File outputFile = new File(destDirectory, entryName); if (entry.isDirectory()) { outputFile.mkdirs(); } else { outputFile.getParentFile().mkdirs(); try (FileOutputStream outputStream = new FileOutputStream(outputFile)) { byte[] buffer = new byte[4096]; int read = 0; while ((read = tarIs.read(buffer)) != -1) { outputStream.write(buffer, 0, read); } } } } } } public static void main(String[] args) throws IOException { String[] sourceFilePaths = new String[] { "/path/to/file1", "/path/to/file2" }; String tarFilePath = "/path/to/archive.tar"; createTarFile(sourceFilePaths, tarFilePath); String destDirectory = "/path/to/destination/dir"; extractTarFile(tarFilePath, destDirectory); } } ``` 在这个示例代码中,`createTarFile` 方法使用 `TarArchiveOutputStream` 创建一个 tar 文件,并将指定的源文件添加到该文件中。对于每个源文件,我们首先创建一个 `TarArchiveEntry`,然后将其添加到输出流中。接下来,我们读取源文件的内容并写入输出流中。最后,关闭所有的输入和输出流。 `extractTarFile` 方法使用 `TarArchiveInputStream` 来解压缩指定的 tar 文件。我们遍历 tar 文件中的每个条目,如果是目录,则创建该目录,否则将条目内容写入输出文件。最后,关闭所有的输入流和输出流。 以上就是使用 Java 实现 tar 文件的压缩和解压缩的示例代码。请注意,在实现 tar 文件的压缩和解压缩时,需要注意文件名和文件路径的编码问题,以避免出现乱码和路径分隔符不一致等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值