
正文
java中复制代码 java复制代码快捷键
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java中复制字符串怎么复制?要求直接输出
只需要赋值给新的字符串变量就可以了。
java中字符串变量修改时,彼此相互不影响。赋值给新的字符串变量时,原来的字符串变量不会随着新字符串变量的修改而修改。
示例:
public void show(){
String s1 = "abc";//初始化一个字符串变量s1
String s2 = s1; //把s1表示的字符串复制给s2
s1="cde";//改变s1的字符串内容
System.out.println(s2);//输出s2,内容还是 "abc",不受原来字符串变量的改变而改变
}
相关问答
Q1: JAVA TXT字符流复制代码问题
File file=new File("E:\\1.txt");//创建文件对象,
FileInputStream fread; //FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。
BufferedInputStream reader=null;
BufferedOutputStream write=null; //该类实现缓冲的输出流。通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统。
try
{
fread = new FileInputStream(file);//将文件对象添加到文件的输入流,也就是你要从你的数据源开始读取数据
reader=new BufferedInputStream(fread);//将字节流对象添加到缓存区中,这样效率要高一点
String str=null;//初始化字符串这样保存的是读取的一行的数据
write=new BufferedOutputStream(new FileOutputStream(new File("E:\\2.txt")));//创建的是输出流缓存 请要输出的目的地添加到缓存区中,
byte b[]=new byte[1024];//定义每次读取的字节的范围,也是存储数据的容器
int i=0;
while((i=reader.read(b))!=-1) //下一个数据字节,如果到达流末尾,则返回 -1。
{
write.write(b);//开始写入数据
}
}
catch (FileNotFoundException e) //如果没有文件打印该异常
{
e.printStackTrace();
}
catch (IOException e) //如果出现io异常 也就处理异常
{
e.printStackTrace();
}
finally//最后不管读取还是没读取成功都释放资源
{
try
{
write.close();//关闭缓存区,关闭缓存区会自动关闭相应的io流
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
Q2: 用java编写的代码复制java程序,结果复制完的java程序中没有保留原java程序中的换行符都把代码写到一行了
骚年,你这个取一行写一行,人家java已经去掉了换行符,你要取一行就在后面加换行符!!
while (scanner.hasNextLine ())
{
fw.write (scanner.nextLine ()+"\r\n");
}
Q3: java编程中怎么复制,粘贴
CTRL+C复制
CTRL+V粘贴
或者选中代码,右键,COPY
要粘贴的地方,右键,P开头那个
Q4: java 里复制和粘贴的功能代码如何实现
1.
往剪切板写文本数据(就是常说的String拉)
Java代码
protected
static
void
setClipboardText(Clipboard
clip,
String
writeMe)
{
Transferable
tText
=
new
StringSelection(writeMe);
clip.setContents(tText,
null);
}
protected
static
void
setClipboardText(Clipboard
clip,
String
writeMe)
{
Transferable
tText
=
new
StringSelection(writeMe);
clip.setContents(tText,
null);
}
2.
从指定的剪切板中获取文本内容
Java代码
protected
static
String
getClipboardText(Clipboard
clip)
throws
Exception{
//
获取剪切板中的内容
Transferable
clipT
=
clip.getContents(null);
if
(clipT
!=
null)
{
//
检查内容是否是文本类型
if
(clipT.isDataFlavorSupported(DataFlavor.stringFlavor))
return
(String)clipT.getTransferData(DataFlavor.stringFlavor);
}
return
null;
}
Q5: java中怎么复制一个文件的内容
主要是用到java里面java中复制代码的i/o流。代码例子如下java中复制代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* java读写文件,复制文件
* 读取d:/1.txt文件内容,写入f:/text.txt文件中.
* @author young
*
*/
public class FileWriterTest {
// 读写文件
public static void rwFile(){
FileWriter fw = null;
BufferedReader br = null;
try {
fw = new FileWriter("f:\\text.txt", true);
br = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\1.txt"), "UTF-8"));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("文件内容: " + line);
fw.write(line);
fw.flush();
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
rwFile();
}
}
首先在D盘新建文件1.txtjava中复制代码,输入任意内容。然后执行java代码即可。
关于java中复制代码和java复制代码快捷键的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






